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

Alembic 1.10.4 incorrectly reflects nulls not distinct #1291

Closed
CaselIT opened this issue Aug 8, 2023 · 4 comments
Closed

Alembic 1.10.4 incorrectly reflects nulls not distinct #1291

CaselIT opened this issue Aug 8, 2023 · 4 comments
Labels

Comments

@CaselIT
Copy link
Member

CaselIT commented Aug 8, 2023

          Hi, I'm not sure this is exactly related but might help.

We have updated to alembic 1.10.4 and we see some changes are always detected and not expected, everytime we generate a new migration.

-- requirements.txt

alembic==1.10.4
SQLAlchemy==2.0.19

-- models.py

from datetime import datetime

from sqlalchemy import UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column, DeclarativeBase

class Base(DeclarativeBase):
    pass

class Exemple(Base):
    __tablename__ = "dummy"
    id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
    a: Mapped[int]
    b: Mapped[int]
    tomb_date: Mapped[datetime] = mapped_column(nullable=True)
    __table_args__ = (
        UniqueConstraint(
            "a",
            "b",
            "tomb_date",
            postgresql_nulls_not_distinct=True,
            name="dummy_a_b_tomb_date_key"
        ),
    )

Initial migration is looking great, and postgresql nulls not distinct works.
But when I rerun autogenerate it always detect the same change.

alembic revision --autogenerate -m "test"
INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
INFO  [alembic.ddl.postgresql] Detected sequence named 'dummy_id_seq' as owned by integer column 'dummy(id)', assuming SERIAL and omitting
INFO  [alembic.autogenerate.compare] Detected changed unique constraint 'dummy_a_b_tomb_date_key' on 'dummy': expression ('UNIQUE_CONSTRAINT', 'a', 'b', 'tomb_date') to ('UNIQUE_CONSTRAINT', 'a', 'b', 'tomb_date', ('nulls_not_distinct', True))
  Generating ... /alembic/versions/733f639ec673_test.py ...  done

It generates a new migration file every time with the same content

"""test

Revision ID: 733f639ec673
Revises: a96502e5e7d3
Create Date: 2023-08-08 17:29:31.612196

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = '733f639ec673'
down_revision: Union[str, None] = 'a96502e5e7d3'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint('dummy_a_b_tomb_date_key', 'dummy', type_='unique')
    op.create_unique_constraint('dummy_a_b_tomb_date_key', 'dummy', ['a', 'b', 'tomb_date'], postgresql_nulls_not_distinct=True)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint('dummy_a_b_tomb_date_key', 'dummy', type_='unique')
    op.create_unique_constraint('dummy_a_b_tomb_date_key', 'dummy', ['a', 'b', 'tomb_date'])
    # ### end Alembic commands ###

Originally posted by @cedric-roussel in #1288 (comment)

@CaselIT CaselIT changed the title Hi, I'm not sure this is exactly related but might help. Alembic 1.10.4 incorrectly reflects nulls not distinct Aug 8, 2023
@CaselIT
Copy link
Member Author

CaselIT commented Aug 8, 2023

@CaselIT
Copy link
Member Author

CaselIT commented Aug 8, 2023

from the log message it seems that the constraint is not detected with null not distinct

@CaselIT
Copy link
Member Author

CaselIT commented Aug 8, 2023

this is completely broken, also the indexes. it seems that fd59164 added tests but they were not very effective ...

@sqla-tester
Copy link
Collaborator

Federico Caselli has proposed a fix for this issue in the main branch:

Fix autogenerate issue in nulls not distinct https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/4797

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

No branches or pull requests

2 participants