piccolo icon indicating copy to clipboard operation
piccolo copied to clipboard

FEATURE: Composite Unique Constraints

Open northpowered opened this issue 3 years ago • 0 comments

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:

  1. Creation with Table (CREATE TABLE)
  2. Creation after Table creation (ALTER TABLE ADD CONSTRAINT)
  3. 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:

  1. ALTER constraint without recreation

Some important notes:

  1. UniqueConstraint class inherit from Column class, so the most part of behavior is like in Column objects
  2. Operations with UC are showing in migration summary like columns (Added colums: ..., Dropped colums:... etc)
  3. I`d used to add extra_imports to auto drop in migrations app to be able to differ usual colums and constraints
  4. The same about DropColumn class, I added column_class param, like in AddColumn
  5. Fixed some tests (DropColumn), because column_class param raised an AssertionError

Help and fixes are welcome!

northpowered avatar Aug 11 '22 08:08 northpowered