0

Why am I getting this error?

  File "/Users/user1/Development/projects/zen/zen_api/zen_api/migrations/versions/ab4404d743ac_.py", line 24, in upgrade
    sa.Column("expires_at", sa.NullType(), nullable=False),
                            ^^^^^^^^^^^
AttributeError: module 'sqlalchemy' has no attribute 'NullType'. Did you mean: 'TupleType'?

I ran flask db migrate to try to push updates to my tables. I noticed that my FIRST versions file was being edited. I was expecting a new file to be created. This is some of the contents of the first file.

enter image description here

"""empty message

Revision ID: ab4404d743ac
Revises: fbaa8bda9a8b
Create Date: 2023-04-26 00:00:29.089871

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "ab4404d743ac"
down_revision = "fbaa8bda9a8b"
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "refresh_token_blocklist",
        sa.Column("jti", sa.UUID(), nullable=False),
        sa.Column("expires_at", sa.NullType(), nullable=False),
        sa.Column("refresh_token", sa.NullType(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.PrimaryKeyConstraint("jti"),
    )
    with op.batch_alter_table("refresh_token_blocklist", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_refresh_token_blocklist_jti"), ["jti"], unique=False
        )

    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table("refresh_token_blocklist", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_refresh_token_blocklist_jti"))

    op.drop_table("refresh_token_blocklist")
    # ### end Alembic commands ###

Second file

"""empty message

Revision ID: fbaa8bda9a8b
Revises: 
Create Date: 2023-04-23 01:07:46.538734

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "fbaa8bda9a8b"
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "property",
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "user",
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("first_name", sa.String(length=128), nullable=False),
        sa.Column("last_name", sa.String(length=128), nullable=False),
        sa.Column("email", sa.String(length=128), nullable=False),
        sa.Column("password_hash", sa.String(length=128), nullable=True),
        sa.Column("phone_number", sa.String(length=10), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    with op.batch_alter_table("user", schema=None) as batch_op:
        batch_op.create_index(batch_op.f("ix_user_email"), ["email"], unique=True)

    op.create_table(
        "review",
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("user_id", sa.UUID(), nullable=True),
        sa.Column("rating", sa.Integer(), nullable=True),
        sa.Column("comment", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["user.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("review")
    with op.batch_alter_table("user", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_user_email"))

    op.drop_table("user")
    op.drop_table("property")
    # ### end Alembic commands ###

I'm using

flask = "^2.2.3"
flask-sqlalchemy = "^3.0.3"
flask-migrate = "^4.0.4"
Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152
Liondancer
  • 15,721
  • 51
  • 149
  • 255
  • 2
    First of all, this is an issue with Alembic, the database migration engine. Second, the file you call "second file" has a date of April 23rd, and the file you call "first file" is from April 26th. So you seem to have them reversed, and also they are both from a couple of weeks ago. Lastly, you have not included in your question how the `expired_at` field is defined in your model. This is the field that is giving you errors, so it is important to see it. – Miguel Grinberg May 10 '23 at 19:34
  • @MiguelGrinberg I think I figured it out. I'm a bit new to `flask-migrate` & `alembic` so I was hesitant to make changes to the `alembic` scripts. I believe my latest `alembic` script is corrupted / out-of-date as I didn't see other DB updates in the script. For example, I was expecting the creation of other tables and column changes but they were not there. I simply deleted `ab4404d743ac_.py` and re-ran `flask db migrate` and got things working :) – Liondancer May 10 '23 at 22:00
  • I'd say the migration scripts were generated from incorrect models, it looks as very same situation as here https://stackoverflow.com/questions/68139087/attributeerror-module-sqlalchemy-has-no-attribute-nulltype – Radoslav Bodó May 11 '23 at 07:56

0 Answers0