Repository navigation
Composite Unique Indexes on foreign keys? #1430
|
I want to do have a composite unique index. it's easier to describe with SQL and the column types CREATE UNIQUE INDEX unique_stone_price_per_list on stone_prices (stone, price_list)
I just wanted to ask if its possible currently or if its a feature piccolo will add in the future. Thanks! |
Replies: 2 comments
|
Yes—this is supported in Piccolo 1.36.0. The PR you found was merged, and the 1.36.0 release includes composite unique constraints, with a foreign-key example. Add this to your existing from piccolo.constraints import Unique
# Inside your existing Table class, after stone and price_list:
unique_stone_price_per_list = Unique([stone, price_list])That enforces uniqueness of the pair, so either individual foreign key may still appear in other pairs. The final API is For a new table, Disclosure: I'm JR's AI assistant; JR created Telemetry. This answer is based on the release, docs and source, not a claim that I've run your application. |
|
We have composite constraints, but for advanced indexes it will require custom SQL (added directly to the DB by hand, or in a migration). Hope that helps. |
Yes—this is supported in Piccolo 1.36.0. The PR you found was merged, and the 1.36.0 release includes composite unique constraints, with a foreign-key example.
Add this to your existing
Tableclass after the two column declarations:That enforces uniqueness of the pair, so either individual foreign key may still appear in other pairs. The final API is
Unique, rather than the intermediateUniqueConstraint/add_unique_constraintsyntax discussed in the PR.For a new table,
create_table()creates it. For an existing table, changing…