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

Missing constraint while creating PK and unique column #1143

Open
mkorlaga opened this issue Jan 16, 2025 · 0 comments
Open

Missing constraint while creating PK and unique column #1143

mkorlaga opened this issue Jan 16, 2025 · 0 comments

Comments

@mkorlaga
Copy link

mkorlaga commented Jan 16, 2025

Problem described causes errors when running migrations and modifying piccolo models because of missing constraint in database (postgres 17).

Tl;tr
Modifying unique argument on a column causes migration to generate dropping constraint that was not created in the first place.

Recreate steps:

  1. Create Table with column A.
  2. Set primary_key = True and unique = True

e.g.
some_uuid_field = UUID(
default=UUID4(),
null=False,
primary_key=True,
unique=True,
index=True,
index_method=IndexMethod.btree,
db_column_name=None,
secret=False,
)

  1. Create and run migration (table and column is created. Notice that in database only 1 constraint is created, which is PK for some_uuid_field column, which is obviously expected behavior)
  2. On model modify unique=True -> unique=False.
  3. Generate migration (notice that we have drop unique constraint step)
  4. Run migration (causes error on trying to drop constraint that doesn't exist as it was not created in first place)

Solution:

  1. When creating a pk column validate if passed arguments contain both pk and unique set to True. If so, then when generating migration for column creation squash unique value as pk is already set to True.
  2. If both values are set to True, in migration create both constraints (doesn't make sense as we already would try to have column to be a pk which should imply being unique)
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