我正在使用sqlalchemy创建一个数据库,该数据库包含三个类,即User,Meeting,MeetingRoom 我想在会议室的会议中创建外键,但是由于某种原因,它会出现以下错误
sqlalchemy.exc.NoReferencedTableError:与列“ meeting.mrid”关联的外键找不到表“ meetingroom”,通过该表生成指向目标列“ mrid”的外键
反之(在会议室添加会议外键)可以正常工作时,我不确定是什么问题 这是代码:
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
答案 0 :(得分:0)
Flask-SQLAlchemy正在使用其自己的表名方案创建表,并且正在使用meeting_room
。因此,您可以在relationship
中引用此名称,也可以通过在模型类上设置__tablename__
属性来覆盖此名称。像这样:
class MeetingRoom (db.Model):
__tablename__ = 'meetingroom'