Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Composite Unique Constraints #583

Open
northpowered opened this issue Aug 11, 2022 · 0 comments
Open

FEATURE: Composite Unique Constraints #583

northpowered opened this issue Aug 11, 2022 · 0 comments

Comments

@northpowered
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant