Skip to content

Server_default including quote not detected properly #1177

@GeorgeSchneelochVA

Description

@GeorgeSchneelochVA

Describe the bug

A column with the server_default (substring(user_name(),charindex('\',user_name())+(1),len(user_name()))) does not compare properly, causing the migration autogenerate command to think that the server default in the model does not match what is on the database.

Expected behavior
A flask db migrate command should not create any new migration if the server_default for a column in the model is the string described above, and that column on the database has the same string for its default constraint.

To Reproduce
Add a table to SQL Server:

CREATE TABLE test1 (id integer primary key, usernamecol varchar(50))

ALTER TABLE test1 ADD  CONSTRAINT [DF_constraint]  DEFAULT (substring(user_name(),charindex('\',user_name())+(1),len(user_name()))) FOR usernamecol

Define the model in SQLAlchemy:

class test1(db.Model):
    __tablename__ = 'test1'
    id = db.Column(db.Integer, primary_key=True)
    usernamecol = db.Column(
        db.VARCHAR(50),
        server_default=sa.text(
            "(substring(user_name(),charindex('\\',user_name())+(1),len(user_name())))"
        )
    )

Run flask db migrate with compare_server_default=True set in your Migrate(...). There will be a migration generated updating the server_default for that column with exactly the same text as was defined in the server_default in the model. The migration should not have been generated since the server default already matches.

Error

# Copy error here. Please include the full stack trace.

Versions.

  • OS: Windows 10
  • Python: 3.8.8
  • Alembic: 1.9.3
  • SQLAlchemy: 2.0.3
  • Database: SQL Server 2019
  • DBAPI:

Additional context
I did some investigation and it looks like the value is handled correctly until this line, where single quotes are added to both sides of the string. I'm not sure the intent of the code there, but this server default should be treated as a SQL expression rather than a string.

Have a nice day!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions