Skip to content

MSSQL needs "NOT NULL" sent explicitly for a change in type to maintain not null #977

@AbdealiLoKo

Description

@AbdealiLoKo

Describe the bug
Looks like in alembic when we do an alter_column() to change the type of a column type_= it is ignoring the existing_nullable=True and removing the nullable.

Expected behavior
I expected the type of my column to change the type - without any other change to the column

To Reproduce
I have a migration like the following in my alembic versions:

"""user_notification: Fix type of title

Revision ID: eb57aaff3a51
Revises: 5a41b8b16e55
Create Date: 2020-03-25 21:00:57.615363

"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = 'eb57aaff3a51'
down_revision = '5a41b8b16e55'
branch_labels = None
depends_on = None

def upgrade():
    with op.batch_alter_table('user_notification') as batch_op:
        batch_op.alter_column(
            'title',
            existing_type=sa.String(length=100),
            type_=sa.String(length=511),
            existing_nullable=False,
        )

def downgrade():
    with op.batch_alter_table('user_notification') as batch_op:
        batch_op.alter_column(
            'title',
            existing_type=sa.String(length=511),
            type_=sa.String(length=100),
            existing_nullable=False,
        )
  • When I do db upgrade 5a41b8b16e55 - The DDL shows: title varchar(100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
  • When I do db upgrade eb57aaff3a51 - The DDL shows: title varchar(511) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    The "NOT NULL" has changed to "NULL" - even though I provided existing_nullable=False

NOTE: I have other cases in my migrations where I only change the name of the column and not the type - I don't notice this behavior at that time. It seems to be only when I make a change to the type.

Versions.
​- OS: CentOS 7
​- Python: 3.6.5
​- Alembic: 1.7.5
​- SQLAlchemy: 1.4.29
​- Database: MSSQL 2019
​- DBAPI: pyodbc

Additional context
Feels like it may be related to this: #812
But I am using the latest version of alembic as of now

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions