Skip to content
Advertisement

sqlalchemy foreign key could not find table

I’m making a database using sqlalchemy which consists of three classes, User, Meeting, MeetingRoom I want to create a foreign key in Meeting for the Meeting room, but for some reason it gives the following error

sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column ‘meeting.mrid’ could not find table ‘meetingroom’ with which to generate a foreign key to target column ‘mrid’

Error screenshot

When we do it the other way around (add a meeting foreign key in meeting room) it works, I’m not sure what’s the problem is This is the code:

JavaScript

Advertisement

Answer

SqlAlchemy is creating the tables with its own tablename scheme and was using meeting_room. So you refer to this name in relationship or you can override by setting the __tablename__ property on the model class. Like this:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement