Error in foreign key constraint with SQLAlchemy

Question:

I am trying to implement very simple example table from an old course now in 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…

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?

Asked By: gsdfhsdfhsdfhsdf

||

Answers:

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:

__table_args__ = (
        ForeignKeyConstraint(
            ['hotel_no', 'room_no'],
            ['rooms.hotel_no', 'rooms.room_no']
        ),
)
Answered By: Adam S