0

I create a new column in a migration and want to directly use it in a query to add records but I get an error.

Migration:

def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('wallets',
    sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
    sa.Column('name', sa.String(), nullable=False),
    sa.Column('user_id', sa.String(), nullable=False),
    sa.ForeignKeyConstraint(['user_id'], ['users.uid'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('name', 'user_id', name='_name_user_id')
    )
    op.create_table('wallet_users',
    sa.Column('wallet_id', postgresql.UUID(), nullable=False),
    sa.Column('user_id', sa.String(), nullable=False),
    sa.Column('added_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['user_id'], ['users.uid'], ),
    sa.ForeignKeyConstraint(['wallet_id'], ['wallets.id'], ),
    sa.PrimaryKeyConstraint('wallet_id', 'user_id')
    )
    # ### end Alembic commands ###

    # create an initial wallet for each user
    bind = op.get_bind()
    session = sa.orm.scoped_session(
            sa.orm.sessionmaker(
                autocommit=False,
                autoflush=False,
                bind=bind,
            ),
        )

    user_repo = UserRepository(session_factory=session)
    wallet_repo = WalletRepository(session_factory=session)

    users = user_repo.get_all()

    wallets = []

    for user in users:
        wallet_repo.add(Wallet(name='initial', user_id=user.uid))

    session.add_all(wallets)

    session.commit()

Error:

sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedColumn) column users.push_token does not exist

This is because the push token is added in a later migration but the sqlalchemy model already has it defined.

So how can I solve this?

Dirk
  • 3,095
  • 4
  • 19
  • 37

0 Answers0