-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Open
Labels
documentationpostgresqlschemathings related to the DDL related objects like Table, Column, CreateIndex, etc.things related to the DDL related objects like Table, Column, CreateIndex, etc.
Milestone
Description
Describe the bug
When inheriting off a postgresql table like this:
CREATE TABLE my_schema.cities (
id int,
name text,
population real,
elevation int -- (in m),
PRIMARY KEY (id),
);
CREATE TABLE my_schema.capitals (
state text UNIQUE NOT NULL
) INHERITS (my_schema.cities);code from https://www.postgresql.org/docs/current/tutorial-inheritance.html.
Alembic generates this upgrade command,
op.create_table(
"capitals"
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("state", sa.Text(), nullable=False),
sa.ForeignKeyConstraint(
["id"],
["my_schema.cities"]
),
sa.UniqueConstraint("state")Emitting this PSQL code,
CREATE TABLE my_schema.capitals (
id INTEGER NOT NULL,
state text UNIQUE NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY(id) REFERENCES my_schema.cities (id),
)
INHERITS ( "my_schema.cities" )Which leads to two errors when it is then pushed to PSQL,
- The table contains a column that already exists in the existing (cities) schema, however to make SQLAlchemy happy you need to have a PK in the table otherwise it throws an error.
- The generated inheritance method should not double quotes, or if it does it should be in this format, i.e.,
"my_schema"."cities"
Expected behaviour
Alembic should recognise that a column is about to be double defined and ignore it from its schema, and also correctly handle double quotes.
To Reproduce
Please try to provide a Minimal, Complete, and Verifiable example, with the migration script and/or the SQLAlchemy tables or models involved.
See also Reporting Bugs on the website.
# Insert code here
Base = declarative_base(metadata=meta)
class Cities(Base):
__tablename__ = "cities"
id: Mapped[int] = mapped_column(
Integer, primary_key=True, autoincrement=True
)
name: Mapped[str] = mapped_column(Text)
population: Mapped[int] = mapped_column(Real)
elevation: Mapped[int] = mapped_column(Integer)
class Capitals(Cities):
__tablename__ = "cities"
id: Mapped[int] = mapped_column(
Integer, primary_key=True, foreign_key="cities.id"
)
state: Mapped[str] = mapped_column(Text, unique=True, nullable=False)
__table_args__ = {"postgresql_inherits": "my_schema.cities"}Error
Produces two PSQL errors,
NOTICE: merging column "id" with inherited definition
ERROR: relation "id" already exists
SQL state: 42P07ERROR: relation "my_schema.cities" does not exist
SQL state: 42P01Versions.
- OS: n/a
- Python: 3.13.7
- Alembic: v1.16.5
- SQLAlchemy: v2.0.43
- Database: PSQL v17
- DBAPI: psycopg v3.2.10
Additional context
Thank you so much for a fantastic project! Have a great day!
Metadata
Metadata
Assignees
Labels
documentationpostgresqlschemathings related to the DDL related objects like Table, Column, CreateIndex, etc.things related to the DDL related objects like Table, Column, CreateIndex, etc.