Adding a Constraint to a table
Check constraints are a way to enforce that only data that meets the constraint is allowed to be added to the table.
Add the Constraint
After you have added the constraint to the table attempting to append data to the table that violates the constraint will instead throw an error.
Verify the constraint by trying to add some data
from deltalake import write_deltalake, DeltaTable
import pandas as pd
dt = DeltaTable("../rust/tests/data/simple_table")
df = pd.DataFrame({"id": [-1]})
write_deltalake(dt, df, mode="append", engine="rust")
# _internal.DeltaProtocolError: Invariant violations: ["Check or Invariant (id > 0) violated by value in row: [-1]"]
let table = deltalake::open_table("../rust/tests/data/simple_table").await?;
let schema = table.get_state().arrow_schema()?;
let invalid_values: Vec<Arc<dyn Array>> = vec![
Arc::new(Int32Array::from(vec![-10]))
];
let batch = RecordBatch::try_new(schema, invalid_values)?;
table.write(vec![batch]).await?;
Note: ensure you use the engine='rust'
parameter when writing to the table as this feature is not supported in the
default pyarrow writer.