繁体   English   中英

FastAPI) SQLAlchemy 外键约束错误

[英]FastAPI) SQLAlchemy Foreign Key Constraint Error

检查了这 4 件事,我似乎没有发现问题:

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

外键:table_entity.db_id FOR Unique Column:db_entity.db_id

db_entity_schema.py(父级)

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(子)

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)

迁移的时候,导致errno: 150 "Foreign key constraint is incorrectly formed"比较费解。

您是否尝试过声明相关对象

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)

话虽如此,您似乎在 DbEntity.seq 上有一个非常好的主键,为什么不将它用作您的外键呢?

这可能不是完整的答案,但我发布这个是为了显示我自动生成的迁移,看看它是否有助于缩小问题的范围。

使用SQLAlchemy 1.4.45 、 alembic 1.9.0pymysql 1.0.2mysql 8.0.31-1.el8 (码头图像)。

我不太了解 mysql 驱动程序,所以我没有偏好。

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 的 env.py

我只是把它放在一起以自动生成一个修订版,所以请原谅它的格式和混乱的导入。

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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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