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:

from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
import os
# ==================================
app = Flask(__name__)
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.db'
app.config['SQLALCHEMY_TRACK_MODIFICATION'] = False
db = SQLAlchemy(app)
ma = Marshmallow(app)
# ====================DATABASE
# =============USER
class User (db.Model):
 id = db.Column(db.Integer,primary_key=True)
 name = db.Column(db.String(30))
 Email = db.Column(db.String(30))
 Pass = db.Column(db.String(30))
 Phone = db.Column(db.String(12))
 Department = db.Column(db.String(30))
 Major = db.Column(db.String(30))
 meeting = db.relationship('Meeting', backref='creator')
 meetingroom = db.relationship('MeetingRoom', backref='reserver')

 def __init__(self, name, Email, Pass, Phone, Department, Major):
  self.name = name
  self.Email = Email
  self.Pass = Pass
  self.Phone = Phone
  self.Department = Department
  self.Major = Major


# ================== MEETING ROOM
class MeetingRoom (db.Model):
 mrid = db.Column(db.Integer,primary_key=True)
 reserver_id = db.Column(db.Integer, db.ForeignKey('user.id'))
 meetingm = db.relationship('Meeting', backref='mrid')
 #mid = db.Column(db.Integer, db.ForeignKey('meeting.Mid'))

 def __init__(self, mrid, reserver_id):
  self.mrid = mrid
  self.reserver_id = reserver_id


# =======================MEETING
class Meeting (db.Model):
 Mid = db.Column(db.Integer,primary_key=True)
 Mname = db.Column(db.String(100))
 Des = db.Column(db.String(100))
 Date = db.Column(db.String(20))
 creator_id = db.Column(db.Integer, db.ForeignKey('user.id'))
 #meetingroomm = db.relationship('MeetingRoom', backref='mroom')
 mrid = db.Column(db.Integer, db.ForeignKey('meetingroom.mrid'))
 #roomno_id = db.Column(db.Integer, db.ForeignKey('meetingroom.mrid')

 def __init__(self, Mname, Des, Date, creator_id, mrid):
  self.Mname = Mname
  self.Des = Des
  self.Date = Date
  self.creator_id = creator_id
  self.mrid = mrid

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:

class MeetingRoom (db.Model):
    __tablename__ = 'meetingroom'
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement