sqlalchemy / sqlalchemy/alembic

alembic autogeneration "false-positively" detects change to comments from the empty string to None

Open
#1,085 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug low priority
Dominant language
Python
Stars
4.4k
Forks
375
PR merge metrics
No merged PRs in 30d

Description

Describe the bug
Setting column comment to "" causes autogenerate to create changes even though no changes were made.

Expected behavior
I expected Column("mycol", sa.types.String(), comment="") to be treated as it is, and that sequential autogenerate commands would generate empty migrations.

To Reproduce
Please try to provide a Minimal, Complete, and Verifiable example, with the migration script and/or the SQLAlchemy tables or models involved.
See also Reporting Bugs on the website.

  • Code:

    class MyTable(Base):
        __tablename__ = "my_table"
        mycol = ColumnNonNull(
            sa.types.String(),
            primary_key=True,
            comment="""
    """.strip(),
        )
    
  • Table generation ok as expected:

    def upgrade() -> None:
        # ### commands auto generated by Alembic - please adjust! ###
        op.create_table(
            "my_table",
            sa.Column("mycol", sa.String(), nullable=False),
            sa.PrimaryKeyConstraint("mycol"),
            schema="public",
        )
        # ### end Alembic commands ###
    
    
    def downgrade() -> None:
        # ### commands auto generated by Alembic - please adjust! ###
        op.drop_table("my_table", schema="public")
        # ### end Alembic commands ###
    
  • Table generation not ok:

    def upgrade() -> None:
        # ### commands auto generated by Alembic - please adjust! ###
        op.alter_column(
            "my_table",
            "mycol",
            existing_type=sa.VARCHAR(),
            comment="",
            existing_nullable=False,
        )
        # ### end Alembic commands ###
    
    
    def downgrade() -> None:
        # ### commands auto generated by Alembic - please adjust! ###
        op.alter_column(
            "my_table",
            "mycol",
            existing_type=sa.VARCHAR(),
            comment=None,
            existing_comment="",
            existing_nullable=False,
        )
        # ### end Alembic commands ###
    
    • Expected:

      def upgrade() -> None:
          # ### commands auto generated by Alembic - please adjust! ###
          pass
          # ### end Alembic commands ###
      
      
      def downgrade() -> None:
          # ### commands auto generated by Alembic - please adjust! ###
          pass
          # ### end Alembic commands ###
      

Error

No errors per se.

Fix
If I set the column to anything other than "" it then starts to work as expected.

Versions.

  • OS: Ubuntu 20.04.5 LTS
  • Python: 3.8
  • Alembic: alembic==1.8.1
  • Database: PostgreSQL

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the PostgreSQL autogeneration case using a column comment set to an empty string, then trace how autogenerate compares the reflected comment with the model comment. Done means sequential autogenerate commands produce empty migrations for this unchanged column, while non-empty comment changes remain detectable.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, python
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.