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

Multiple Unique Constraints with auto migrations #582

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

northpowered
Copy link

Full description is in pinned issue
ADDED:

  1. UniqueConstraint Class with the same behavior as a Column
  2. CREATE and DROP auto migrations
  3. A bit of doc for a UniqueConstraint Class
  4. Updated tests

IMPORTANT!
There`re no ALTER CONSTRAINT option, but will be added soon, I hope.
For changing constraint I recommend do it with 2 migrations: DROP->CREATE

@northpowered northpowered changed the title Multiple Unique Constraints with auto mirtaions Multiple Unique Constraints with auto migrations Aug 11, 2022
@northpowered northpowered marked this pull request as ready for review August 11, 2022 08:30
@dantownsend
Copy link
Member

@northpowered Thanks for this, it looks promising!

I think the linters are currently failing because of isort.

@northpowered
Copy link
Author

@northpowered Thanks for this, it looks promising!

I think the linters are currently failing because of isort.

Mb because we are using different linters
Due to testing, I failed only one test, but it`s the similar to the beginning of my work :D
Also #583 has some details

ALTER TABLE foo_table ADD CONSTRAINT my_constraint_1 UNIQUE (foo_field, bar_field);
ALTER TABLE foo_table DROP IF EXIST CONSTRAINT my_constraint_1;
"""
def __init__(self, unique_columns: list[str]) -> None:
Copy link
Member

@dantownsend dantownsend Oct 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is why the tests are failing.

We can't use this newer syntax yet, because we're keeping backwards compatibility with Python 3.7.

So it'll have to be typing.List[str] instead.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I used to Python3.10, so I forgot about older versions

__slots__ = ("constraint_name","columns")

constraint_name: str
columns: list[str]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above -> List[str]

@@ -433,6 +446,12 @@ def _get_constraint_name(self, column: t.Union[str, ForeignKey]) -> str:
tablename = self.table._meta.tablename
return f"{tablename}_{column_name}_fk"

def add_unique_constraint(self, constraint_name: str, columns: list[str]):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

@dantownsend
Copy link
Member

dantownsend commented Oct 14, 2022

@northpowered Sorry it has taken me so long - let's try and get these tests passing.

@northpowered
Copy link
Author

@dantownsend
I changed list[...] to t.List[...] to keep this compatible with Python3.7
Also upgraded some files up to v.0.109.0

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

Successfully merging this pull request may close these issues.

2 participants