简体   繁体   中英

FastAPI) SQLAlchemy Foreign Key Constraint Error

Checked for these 4 things, and I don't seem to see issues:

1. Same data type
2. Same nullable designation
3. Foreign Key being assigned to PK or Unique column 
4. Same Charset for both 

Foreign Key: table_entity.db_id FOR Unique Column: db_entity.db_id

db_entity_schema.py (Parent)

from sqlalchemy import Column, String, SMALLINT
from sqlalchemy.dialects.mysql import SMALLINT, TINYINT, BIGINT, CHAR

class DbEntity(Base=declartive_base):
    __tablename__ = "db_entity"

    seq = Column(BIGINT(20), primary_key=True, autoincrement=True, nullable=False)
    db_id = Column(String(24), unique=True, nullable=False, comment="DB ID")
    db_service_id = Column(String(24), nullable=False)

table_entity_schema.py (Child)

from sqlalchemy import Column, String, ForeignKey
from sqlalchemy.dialects.mysql import SMALLINT, TINYINT, BIGINT
from sqlalchemy import UniqueConstraint

class TableEntity(Base=declartive_base): 
    __tablename__ = "table_entity"
    __table_args__ = (UniqueConstraint("schema_name", "table_name", name="schema_table_uq_constraint"),)

    table_no = Column(BIGINT(20), primary_key=True, autoincrement=True, nullable=False)
    db_id = Column(String(24), ForeignKey("db_entity.db_id"), nullable=False, comment="DB ID")
    db_service_id = Column(String(24), nullable=False)
    schema_name = Column(String(128), nullable=False)
    table_name = Column(String(128), nullable=False)

When migrating, resulting in a errno: 150 "Foreign key constraint is incorrectly formed" which is quite puzzling.

Have you tried to declare the related object like

from sqlalchemy import Column, String, ForeignKey
from sqlalchemy.dialects.mysql import SMALLINT, TINYINT, BIGINT
from sqlalchemy import UniqueConstraint

class TableEntity(Base=declartive_base): 
    __tablename__ = "table_entity"
    __table_args__ = (UniqueConstraint("schema_name", "table_name", name="schema_table_uq_constraint"),)

    table_no = Column(BIGINT(20), primary_key=True, autoincrement=True, nullable=False)
    db_id = Column(String(24), ForeignKey(DbEntity.db_id), nullable=False, comment="DB ID")
    db_service_id = Column(String(24), nullable=False)
    schema_name = Column(String(128), nullable=False)
    table_name = Column(String(128), nullable=False)

With that said, it seems like you have a perfectly good primary key on the DbEntity.seq, why not use that for your foreign key?

This probably isn't the complete answer but I'm posting this to display the migration I autogenerated to see if it helps narrow down the issue.

Using SQLAlchemy 1.4.45 , alembic 1.9.0 , pymysql 1.0.2 and mysql 8.0.31-1.el8 (docker image).

I don't know much about mysql drivers so I have no preference.

771ce744af5d_.py

"""empty message

Revision ID: 771ce744af5d
Revises: 
Create Date: 2022-12-22 02:11:08.325864

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql

# revision identifiers, used by Alembic.
revision = '771ce744af5d'
down_revision = None
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('db_entity',
    sa.Column('seq', mysql.BIGINT(display_width=20), autoincrement=True, nullable=False),
    sa.Column('db_id', sa.String(length=24), nullable=False, comment='DB ID'),
    sa.Column('db_service_id', sa.String(length=24), nullable=False),
    sa.PrimaryKeyConstraint('seq'),
    sa.UniqueConstraint('db_id'),
    schema='testdb'
    )
    op.create_table('table_entity',
    sa.Column('table_no', mysql.BIGINT(display_width=20), autoincrement=True, nullable=False),
    sa.Column('db_id', sa.String(length=24), nullable=False, comment='DB ID'),
    sa.Column('db_service_id', sa.String(length=24), nullable=False),
    sa.Column('schema_name', sa.String(length=128), nullable=False),
    sa.Column('table_name', sa.String(length=128), nullable=False),
    sa.ForeignKeyConstraint(['db_id'], ['testdb.db_entity.db_id'], ),
    sa.PrimaryKeyConstraint('table_no'),
    sa.UniqueConstraint('schema_name', 'table_name', name='schema_table_uq_constraint'),
    schema='testdb'
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('table_entity', schema='testdb')
    op.drop_table('db_entity', schema='testdb')
    # ### end Alembic commands ###

Alembic's env.py

I just slopped this together to autogenerate a revision so please excuse it's formatting and messy imports.

from logging.config import fileConfig

from sqlalchemy import engine_from_config
from sqlalchemy import pool

from alembic import context

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config

# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
    fileConfig(config.config_file_name)

from sqlalchemy import (
    Integer,
    String,
    ForeignKey,
    UniqueConstraint,
)
from sqlalchemy.schema import (
    Column,
)
from sqlalchemy.orm import backref, relationship, declarative_base, Session
from sqlalchemy import create_engine, MetaData, Column, ForeignKey, Integer, String

Base = declarative_base(metadata=MetaData(schema="testdb"))

from sqlalchemy import Column, String, SMALLINT
from sqlalchemy.dialects.mysql import SMALLINT, TINYINT, BIGINT, CHAR

class DbEntity(Base):
    __tablename__ = "db_entity"

    seq = Column(BIGINT(20), primary_key=True, autoincrement=True, nullable=False)
    db_id = Column(String(24), unique=True, nullable=False, comment="DB ID")
    db_service_id = Column(String(24), nullable=False)


class TableEntity(Base):
    __tablename__ = "table_entity"
    __table_args__ = (UniqueConstraint("schema_name", "table_name", name="schema_table_uq_constraint"),)

    table_no = Column(BIGINT(20), primary_key=True, autoincrement=True, nullable=False)
    db_id = Column(String(24), ForeignKey("db_entity.db_id"), nullable=False, comment="DB ID")
    db_service_id = Column(String(24), nullable=False)
    schema_name = Column(String(128), nullable=False)
    table_name = Column(String(128), nullable=False)

# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = Base.metadata

# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.


def run_migrations_offline() -> None:
    """Run migrations in 'offline' mode.

    This configures the context with just a URL
    and not an Engine, though an Engine is acceptable
    here as well.  By skipping the Engine creation
    we don't even need a DBAPI to be available.

    Calls to context.execute() here emit the given string to the
    script output.

    """
    url = config.get_main_option("sqlalchemy.url")
    context.configure(
        url=url,
        target_metadata=target_metadata,
        literal_binds=True,
        dialect_opts={"paramstyle": "named"},
    )

    with context.begin_transaction():
        context.run_migrations()


def run_migrations_online() -> None:
    """Run migrations in 'online' mode.

    In this scenario we need to create an Engine
    and associate a connection with the context.

    """
    connectable = engine_from_config(
        config.get_section(config.config_ini_section),
        prefix="sqlalchemy.",
        poolclass=pool.NullPool,
    )

    with connectable.connect() as connection:
        context.configure(
            connection=connection, target_metadata=target_metadata
        )

        with context.begin_transaction():
            context.run_migrations()


if context.is_offline_mode():
    run_migrations_offline()
else:
    run_migrations_online()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM