piccolo
piccolo copied to clipboard
FEATURE: Composite Unique Constraints
Previous issues #572 #172 Previous discussions #175
Finally, some result!
New feature: Composite UNIQUE CONSTRAINT
Pull request #582
Usage:
from piccolo.colums.constraints import UniqueConstraint
class FooTable(Table):
foo_field = Text()
bar_field = Text()
my_constraint_1 = UniqueConstraint(['foo_field','bar_field'])
or multiple constraints:
from piccolo.colums.constraints import UniqueConstraint
class FooTable(Table):
foo_field = Text()
bar_field = Text()
spam_field = Text()
eggs_field = Text()
my_constraint_1 = UniqueConstraint(['foo_field','bar_field'])
my_constraint_2 = UniqueConstraint(['spam_field','eggs_field'])
Auto migrations are working for:
- Creation with Table (CREATE TABLE)
- Creation after Table creation (ALTER TABLE ADD CONSTRAINT)
- Drop (DROP CONSTRAINT)
If You want to update constraint (ex. add new columns to UniqueConstraint()), You should, at first DROP, and then CREATE the new one. Example:
#Comment the line
#my_constraint_1 = UniqueConstraint(['foo_field','bar_field'])
>> ...migrate...
#Uncomment the line
my_constraint_1 = UniqueConstraint(['foo_field','spam_field'])
>> ...migrate...
In progress:
- ALTER constraint without recreation
Some important notes:
- UniqueConstraint class inherit from Column class, so the most part of behavior is like in Column objects
- Operations with UC are showing in migration summary like columns (Added colums: ..., Dropped colums:... etc)
- I`d used to add extra_imports to auto drop in migrations app to be able to differ usual colums and constraints
- The same about DropColumn class, I added column_class param, like in AddColumn
- Fixed some tests (DropColumn), because column_class param raised an AssertionError
Help and fixes are welcome!