简体   繁体   English

SQLAlchemy的外键约束错误

[英]Error in foreign key constraint with SQLAlchemy

I am trying to implement very simple example table from an old course now in SQLAlchemy... 我现在正在尝试从SQLAlchemy的一门老课程中实现非常简单的示例表...

I have got this far but when I run the code... 我已经走了这么远,但是当我运行代码时...

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Date, MetaData
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy import create_engine

Base = declarative_base()
engine = create_engine('mysql://x @ amazonaws.com:3306/db', echo=True)
class Guest(Base):
    __tablename__ = "guests"
    guest_no = Column(String(4), primary_key=True)
    g_name = Column(String(20))
    g_address = Column(String(30))
    booking = relationship("Booking", back_populates="guests")
class Hotel(Base):
    __tablename__ = "hotels"
    hotel_no = Column(String(4), primary_key=True)
    h_name = Column(String(20))
    h_address = Column(String(30))
    room = relationship("Room", back_populates="hotels")
    booking = relationship("Booking", back_populates="hotels")
class Room(Base):
    __tablename__ = "rooms"
    hotel_no = Column(String(4), ForeignKey('hotels.hotel_no'), primary_key=True)
    room_no = Column(String(4), primary_key=True)
    r_type = Column(String(1))
    r_price = Column(Integer)
    hotel = relationship("Hotel", back_populates="rooms")
    booking = relationship("Booking", back_populates="rooms")
class Booking(Base):
    __tablename__ = "bookings"
    hotel_no = Column(String(4), ForeignKey('hotels.hotel_no'),  primary_key=True)
    guest_no = Column(String(4), ForeignKey('guests.guest_no'), primary_key=True)
    date_form = Column(Date, primary_key=True)
    date_to = Column(Date)
    room_no = Column(String(4), ForeignKey('rooms.room_no'), primary_key=True)
    hotel = relationship("Hotel", back_populates="bookings")
    guest = relationship("Guest", back_populates="bookings")
    room = relationship("Room", back_populates="bookings")

Base.metadata.create_all(engine)

it gives me an error about the room_no foreign key... 它给我关于room_no外键的错误...

2017-09-11 16:16:03 2b8010c29700 Error in foreign key constraint of table db/bookings:
FOREIGN KEY(room_no) REFERENCES rooms (room_no)
):
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html
for correct foreign key definition.

I looked around a bit and I made sure they were both the same type (they were) and were both primary keys (they previously were not) but the error persists. 我四处张望,并确保它们都是相同的类型(它们是)并且都是主键(以前不是),但是错误仍然存​​在。

Does anyone have insight into what is causing this? 有谁了解导致这种情况的原因?

Because rooms has a composite primary key: (hotel_no, room_no) you'll need to specify both columns in your foreign key relationship on the booking table: 由于房间具有复合主键:(hotel_no,room_no),因此需要在预订表上的外键关系中指定两列:

__table_args__ = (
        ForeignKeyConstraint(
            ['hotel_no', 'room_no'],
            ['rooms.hotel_no', 'rooms.room_no']
        ),
)

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

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