Skip to content

Incorrect PSQL Quotes for postgresql_inherits (document quoted_name for this, and also clean up old things in quoted_name docs) #12877

@marcuswishart

Description

@marcuswishart

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,

  1. 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.
  2. 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: 42P07
ERROR:  relation "my_schema.cities" does not exist 

SQL state: 42P01

Versions.

  • 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

No one assigned

    Labels

    documentationpostgresqlschemathings related to the DDL related objects like Table, Column, CreateIndex, etc.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions