Alembic Autogenerate Creates New Table Scripts Instead of Adding Column to Existing Table #1416
-
Describe the bug Expected behavior
import os
import pytz
from sqlalchemy import (Boolean, Column, DateTime, Integer, MetaData,
PrimaryKeyConstraint, String, Enum)
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.sql import func
from app.core.enums import ExerciseDeliveryMode
SCHEMA_NAME = os.environ["SCHEMA_NAME"]
timezone = pytz.timezone("Asia/Kolkata")
class Base(DeclarativeBase):
metadata = MetaData(schema=SCHEMA_NAME)
class TimestampMixin(object):
created_at = Column(DateTime, server_default=func.now(), nullable=False)
updated_at = Column(
DateTime,
nullable=False,
)
class QuestionAttempt(Base):
__tablename__ = "question_attempt"
id = Column(Integer, primary_key=True)
question_id = Column(Integer, nullable=False)
exercise_id = Column(Integer, nullable=False)
student_id = Column(Integer, nullable=False)
is_correct = Column(Boolean, nullable=False)
timestamp = Column(DateTime, nullable=False, server_default=func.now())
duration = Column(Integer, nullable=True)
answer = Column(String(100), nullable=True)
class ExerciseProgress(Base):
__tablename__ = "exercise_progress"
student_id = Column(Integer)
exercise_id = Column(Integer)
questions_attempted = Column(Integer)
questions_correct = Column(Integer)
mastery_level = Column(Integer, nullable=False, server_default="0")
last_attempted = Column(DateTime, nullable=False, server_default=func.now())
mastery_achieved = Column(Boolean, nullable=False, server_default="false")
mastery_achieved_at = Column(DateTime, nullable=True)
# create composite primary key
__table_args__ = (
PrimaryKeyConstraint("student_id", "exercise_id"),
{},
)
import os
import pytz
from sqlalchemy import (Boolean, Column, DateTime, Integer, MetaData,
PrimaryKeyConstraint, String, Enum)
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.sql import func
from app.core.enums import ExerciseDeliveryMode
SCHEMA_NAME = os.environ["SCHEMA_NAME"]
timezone = pytz.timezone("Asia/Kolkata")
class Base(DeclarativeBase):
metadata = MetaData(schema=SCHEMA_NAME)
class TimestampMixin(object):
created_at = Column(DateTime, server_default=func.now(), nullable=False)
updated_at = Column(
DateTime,
nullable=False,
)
class QuestionAttempt(Base):
__tablename__ = "question_attempt"
id = Column(Integer, primary_key=True)
question_id = Column(Integer, nullable=False)
exercise_id = Column(Integer, nullable=False)
student_id = Column(Integer, nullable=False)
is_correct = Column(Boolean, nullable=False)
timestamp = Column(DateTime, nullable=False, server_default=func.now())
duration = Column(Integer, nullable=True)
answer = Column(String(100), nullable=True)
class ExerciseProgress(Base):
__tablename__ = "exercise_progress"
student_id = Column(Integer)
exercise_id = Column(Integer)
questions_attempted = Column(Integer)
questions_correct = Column(Integer)
mastery_level = Column(Integer, nullable=False, server_default="0")
last_attempted = Column(DateTime, nullable=False, server_default=func.now())
mastery_achieved = Column(Boolean, nullable=False, server_default="false")
mastery_achieved_at = Column(DateTime, nullable=True)
# Enum
delivery_mode = Column(
Enum(ExerciseDeliveryMode, name="exercise_delivery_mode"),
nullable=False,
server_default=ExerciseDeliveryMode.SIMPLE.value,
)
# create composite primary key
__table_args__ = (
PrimaryKeyConstraint("student_id", "exercise_id"),
{},
)
"""Add delivery_mode column
Revision ID: a0db00ab8187
Revises: 3e9083e86083
Create Date: 2024-02-06 10:37:43.797500
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = 'a0db00ab8187'
down_revision: Union[str, None] = '3e9083e86083'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('exercise_progress',
sa.Column('student_id', sa.Integer(), nullable=False),
sa.Column('exercise_id', sa.Integer(), nullable=False),
sa.Column('questions_attempted', sa.Integer(), nullable=True),
sa.Column('questions_correct', sa.Integer(), nullable=True),
sa.Column('mastery_level', sa.Integer(), server_default='0', nullable=False),
sa.Column('last_attempted', sa.DateTime(), server_default=sa.text('now()'), nullable=False),
sa.Column('mastery_achieved', sa.Boolean(), server_default='false', nullable=False),
sa.Column('mastery_achieved_at', sa.DateTime(), nullable=True),
sa.Column('delivery_mode', sa.Enum('SIMPLE', 'ADAPTIVE', name='exercise_delivery_mode'), server_default='simple', nullable=False),
sa.PrimaryKeyConstraint('student_id', 'exercise_id'),
schema='exercise'
)
op.create_table('question_attempt',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('question_id', sa.Integer(), nullable=False),
sa.Column('exercise_id', sa.Integer(), nullable=False),
sa.Column('student_id', sa.Integer(), nullable=False),
sa.Column('is_correct', sa.Boolean(), nullable=False),
sa.Column('timestamp', sa.DateTime(), server_default=sa.text('now()'), nullable=False),
sa.Column('duration', sa.Integer(), nullable=True),
sa.Column('answer', sa.String(length=100), nullable=True),
sa.PrimaryKeyConstraint('id'),
schema='exercise'
)
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('question_attempt', schema='exercise')
op.drop_table('exercise_progress', schema='exercise')
# ### end Alembic commands ### Versions.
Have a nice day! |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 10 replies
-
Hi, The most likely reason is that the scheme name in your env variable has changed. |
Beta Was this translation helpful? Give feedback.
-
Yes |
Beta Was this translation helpful? Give feedback.
-
Today again , in another project first i created an initial migration script to create 3 tables then modified a string column in a table and now when i autogenerated the alembic generated the script to create all 3 tables again |
Beta Was this translation helpful? Give feedback.
-
has anybody found a fix im also having the same issue I have been removing the migration folder and it seems to return back to the directory which keeps me from using these commands flask db migrate -m "Initial migration.", flask db upgrade in admin cmd it always returns with this error code. ERROR [flask_migrate] Error: Can't locate revision identified by '9ebf91bbcd22' |
Beta Was this translation helpful? Give feedback.
the env.py file is missing from that repository, but I'm assuming it's the default one generated by alembic.
You have to add
include_schemas=True
tocontext.configure
since you are using schemas@zzzeek it seems strange though that if include schema is not set the compare is not happening correctly