Skip to content

Commit 810afe2

Browse files
committed
Tests: Test composite synthetic uniqueness constraints
1 parent 435ec4e commit 810afe2

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

src/sqlalchemy_cratedb/support/polyfill.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ def check_uniqueness_factory(sa_entity, *attribute_names):
3939
4040
This is used by CrateDB's MLflow adapter.
4141
42+
TODO: Maybe add to some helper function?
4243
TODO: Maybe enable through a dialect parameter `crate_polyfill_unique` or such.
44+
TODO: Maybe derive from the model definition itself?
45+
__table_args__ = (sa.UniqueConstraint("name", "user_id", name="unique_name_user"),)
4346
""" # noqa: E501
4447

4548
# Synthesize a canonical "name" for the constraint,

tests/test_support_polyfill.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class FooBar(Base):
8484
name = sa.Column(sa.String)
8585

8686
# Add synthetic UNIQUE constraint on `name` column.
87-
listen(FooBar, "before_insert", check_uniqueness_factory(FooBar, "name"))
87+
listen(FooBar, "before_insert", check_uniqueness_factory(FooBar, "id", "name"))
8888

8989
Base.metadata.drop_all(engine, checkfirst=True)
9090
Base.metadata.create_all(engine, checkfirst=True)
@@ -96,11 +96,11 @@ class FooBar(Base):
9696
session.execute(sa.text("REFRESH TABLE foobar"))
9797

9898
# Insert second record, violating the uniqueness constraint.
99-
bar_item = FooBar(id="bar", name="foo")
99+
bar_item = FooBar(id="foo", name="foo")
100100
session.add(bar_item)
101101
with pytest.raises(IntegrityError) as ex:
102102
session.commit()
103-
assert ex.match("DuplicateKeyException in table 'foobar' on constraint 'name'")
103+
assert ex.match("DuplicateKeyException in table 'foobar' on constraint 'id-name'")
104104

105105

106106
@pytest.mark.skipif(

0 commit comments

Comments
 (0)