Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use SQLAlchemy Utils in a SQLAlchemy model

I'm trying to create a user model that uses UUID as primary key:

from src.db import db # SQLAlchemy instance

import sqlalchemy_utils

import uuid


class User(db.Model):
    __tablename__ = 'user'

    id = db.Column(sqlalchemy_utils.UUIDType(binary=True), primary_key=True, nullable=False)

But when I generate the migrations I receive:

File "/home/pc/Downloads/project/auth/venv/lib/python3.6/site-packages/alembic/runtime/environment.py", line 836, in run_migrations
    self.get_context().run_migrations(**kw)
  File "/home/pc/Downloads/project/auth/venv/lib/python3.6/site-packages/alembic/runtime/migration.py", line 330, in run_migrations
step.migration_fn(**kw)
  File "/home/pc/Downloads/project/auth/migrations/versions/efae4166f832_.py", line 22, in upgrade
    sa.Column('id', sqlalchemy_utils.types.uuid.UUIDType(length=16), nullable=False),
NameError: name 'sqlalchemy_utils' is not defined`

I had try to explicity inform the module I'm using like this and use a 'internal' implementation that SQLAlchemy

Obs: If I manualy import the sqlalchemy_utils in the /migrations/version/efae4166f832_.py and remove the length that is generated automaticaly sa.Column('id', sqlalchemy_utils.types.uuid.UUIDType(length=16), nullable=False) it works fine

I generate the migrations using a generate.py script:

from src import create_app

from src.db import db

from flask_migrate import Migrate

# Models

from src.user.models.user import User

app = create_app()

migrate = Migrate(app, db)`

enter image description here

Obs: MySQL engine

I expect that when I generate migration it generate a user model that uses UUID implemented from SQLAlchemy Utils as primary key


2 Answers

You have just to add:

import sqlalchemy_utils

to your script.py.mako inside migrations folder

like image 62
Marco Caggiano Avatar answered Aug 28 '26 10:08

Marco Caggiano


Background

It would be ideal if you didn't have to go and manually edit each migration file with an import sqlalchemy_utils statement.

Looking at the Alembic documentation, script.py.mako is "a Mako template file which is used to generate new migration scripts." Therefore, you'll need to re-generate your migration files, with Mako already importing sqlalchemy_utils as part of the migration file generation.

Fix

If possible, remove your old migrations (they're probably broken anyway), and add the import sqlalchemy_utils like so to your script.py.mako file:

from alembic import op
import sqlalchemy as sa
import sqlalchemy_utils #<-- line you add
${imports if imports else ""}

then just rerun your alembic migrations:

alembic revision --autogenerate -m "create initial tables"

when you go to look at your migration file you should see sqlalchemy_utils already imported via the mako script.

hope that helps.

like image 29
AndrewO Avatar answered Aug 28 '26 08:08

AndrewO



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!