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 does not carry the uuid format parameter #59

Closed
hidaris opened this issue Nov 30, 2020 · 2 comments
Closed

Alembic does not carry the uuid format parameter #59

hidaris opened this issue Nov 30, 2020 · 2 comments
Labels
bug Something isn't working

Comments

@hidaris
Copy link

hidaris commented Nov 30, 2020

Thanks for previous fixes for 57, After that I found out that alembic does not carry the uuid format parameter when creating the migration file for uuid.

generate:

op.create_table('usermodel',
    sa.Column('id', ormar.fields.sqlalchemy_uuid.UUID(), nullable=False),

should be:

op.create_table('usermodel',
    sa.Column('id', ormar.fields.sqlalchemy_uuid.UUID(uuid_format="string"), nullable=False),
@collerek collerek added the bug Something isn't working label Nov 30, 2020
@collerek
Copy link
Owner

Actually it should be just

  • for hex: sa.Column('id', sa.CHAR(32), nullable=False),
  • for string: sa.Column('id', sa.CHAR(36), nullable=False),

I changed the column repr method to return char.

It should not use ormar at all but unfortunately by default alembic uses full module name for autoganeration.

To change it you have to pass user_module_prefix='sa.' parameter to context.configure in alembic env.py.
The sa. is the default sqlalchemy prefix, that you can also configure it so if you change it to something different in alembic configuration (sqlalchemy_module_prefix parameter) this one have to match.

"""
***Beginning of the sample revision file***
Revision ID: XXX
Revises: 
Create Date: XXXX-XX-XX

"""
from alembic import op
# alembic by default imports sqlalchemy as sa
# this has to match the set prefix ('sa.' in example above)
import sqlalchemy as sa   

So it should look like this:

(<===trimmed for clarity===>)
def run_migrations_offline():
    """Run migrations in 'offline' mode.

    This configures the context with just a URL
    and not an Engine, though an Engine is acceptable
    here as well.  By skipping the Engine creation
    we don't even need a DBAPI to be available.

    Calls to context.execute() here emit the given string to the
    script output.

    """
    context.configure(
        url=URL,
        target_metadata=target_metadata,
        literal_binds=True,
        dialect_opts={"paramstyle": "named"},
        # if you use UUID field set also this param
        # the prefix has to match sqlalchemy import name in alembic
        # that can be set by sqlalchemy_module_prefix option (default 'sa.')
        user_module_prefix='sa.'
    )

    with context.begin_transaction():
        context.run_migrations()


def run_migrations_online():
    """Run migrations in 'online' mode.

    In this scenario we need to create an Engine
    and associate a connection with the context.

    """
    connectable = create_engine(URL)

    with connectable.connect() as connection:
        context.configure(
            connection=connection,
            target_metadata=target_metadata,
            # if you use UUID field set also this param
            # the prefix has to match sqlalchemy import name in alembic
            # that can be set by sqlalchemy_module_prefix option (default 'sa.')
            user_module_prefix='sa.'
        )

        with context.begin_transaction():
            context.run_migrations()
(<===trimmed for clarity===>)

Please update to 0.5.5 for this feature - also included this in the alembic docs.

@hidaris
Copy link
Author

hidaris commented Nov 30, 2020

I see the problem now, thanks for the quick fix and instructions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants