Skip to content

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

DeltaTable

from deltalake import DeltaTable

dt = DeltaTable("../rust/tests/data/simple_table")

# Check the schema before hand
print(dt.schema())
# Add the constraint to the table.
dt.alter.add_constraint({"id_gt_0": "id > 0"})

DeltaTable

let table = deltalake::open_table("../rust/tests/data/simple_table").await?;
let ops = DeltaOps(table);
ops.with_constraint("id_gt_0", "id > 0").await?;

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.