Describe the bug
When using Alembic's revision --autogenerate command to generate a migration script after adding a new column to an existing table, Alembic incorrectly generates a script that attempts to create new tables for all the models defined in the SQLAlchemy models file, instead of generating a migration script that simply adds the new column to the specified table. This behavior occurs even though the tables already exist in the database, and the only change made was the addition of a single column to one table.
Expected behavior
The expected behavior is for Alembic to generate a migration script that includes an operation to add the newly defined column to the existing table in the database. There should be no operations in the migration script related to creating new tables, especially since these tables already exist in the database and no changes have been made to them. The autogenerated script should only reflect the addition of the new column.
To Reproduce
Describe the bug When using Alembic's revision --autogenerate command to generate a migration script after adding a new column to an existing table, Alembic incorrectly generates a script that attempts to create new tables for all the models defined in the SQLAlchemy models file, instead of generating a migration script that simply adds the new column to the specified table. This behavior occurs even though the tables already exist in the database, and the only change made was the addition of a single column to one table.
Expected behavior The expected behavior is for Alembic to generate a migration script that includes an operation to add the newly defined column to the existing table in the database. There should be no operations in the migration script related to creating new tables, especially since these tables already exist in the database and no changes have been made to them. The autogenerated script should only reflect the addition of the new column. To Reproduce
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
Versions.
Have a nice day!