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

postgresql.UUID server default always compares to false, thereby always autogenerating #365

Closed
sqlalchemy-bot opened this issue Mar 21, 2016 · 2 comments

Comments

@sqlalchemy-bot
Copy link

Migrated issue, originally created by Peter Lada (@peterlada)

PROBLEM

My sqlalchemy model:

class Issue(Base):
    __tablename__ = 'issue'
    id = Column(Integer, primary_key=True) 
    ...
    uuid = Column(postgresql.UUID, nullable=False, index=True, default=uuid.uuid4(), server_default=text("uuid_generate_v4()"))

Configured env.py with compare_server_default=True

Yields a autogenerated revision each time, setting the server_default=sa.Text('uuid_generate_v4()')

ROOT CAUSE

Comparing the server defaults with:

SELECT uuid_generate_v4() = uuid_generate_v4() yields False (with an exceptionally high probability).

WORKAROUND

def my_compare_server_default(context, inspected_column, metadata_column,
                              inspected_default, metadata_default, rendered_metadata_default):
    if type(inspected_column.type).__name__ == 'UUID':
        if inspected_default == rendered_metadata_default:
            print("they are the same")
            return False  # unintuitive: no need for migration
        else:
            return True  # unintuitive: needs a migration
    return None

and in env.py: compare_server_default=my_compare_server_default

@sqlalchemy-bot
Copy link
Author

Michael Bayer (@zzzeek) wrote:

  • Added a fix to Postgresql server default comparison which first checks
    if the text of the default is identical to the original, before attempting
    to actually run the default. This accomodates for default-generation
    functions that generate a new value each time such as a uuid function.
    fixes postgresql.UUID server default always compares to false, thereby always autogenerating #365
  • test against uuid_generate_v4() directly, but this requires extensions
    to be installed. should come up with a built in function for this test

9538c3e

@sqlalchemy-bot
Copy link
Author

Changes by Michael Bayer (@zzzeek):

  • changed status to closed

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

No branches or pull requests

1 participant