这是我正在尝试的代码,用于创建迁移文件但无法升级。
ConvMaster [将其视为书名] 以其ID作为外键进入响应主控[本章内容]
IntentMaster [将其视为章节名称]
RespMaster [本章内容]
ChatHistory [类似于记录库,它记录,阅读了哪些内容,从哪一章和从哪本书开始>
为了便于操作,我想将记录保留在ChatHistory中,而不是从RespMaster查询它。 错误-与外键约束有关
请提出如何实现此目标,或者仅是查询的方法?
class ConvMaster(db.Model):
__tablename__ = 'convmaster'
id = db.Column(db.Integer, primary_key=True)
convName = db.Column(db.String(60), unique = True)
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
resp = db.relationship('RespMaster', backref='conv', lazy='dynamic')
chat = db.relationship('ChatHistory', backref='chatconv', lazy='dynamic')
def __repr__(self):
return '<ConvMaster: {}>'.format(self.convName)
class IntentMaster(db.Model):
__tablename__ = 'intentmaster'
id = db.Column(db.Integer, primary_key=True)
intentName = db.Column(db.String(60), unique = True)
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
resp = db.relationship('RespMaster', backref='intent', lazy='dynamic')
chat = db.relationship('ChatHistory', backref='chatintent', lazy='dynamic')
def __repr__(self):
return '<Intent_Master: {}>'.format(self.intentName)
class RespMaster(db.Model):
__tablename__ = 'respmaster'
id = db.Column(db.Integer, primary_key=True)
convID = db.Column(db.Integer, db.ForeignKey('convmaster.id'), nullable=False)
intentID = db.Column(db.Integer, db.ForeignKey('intentmaster.id'), nullable=False)
resp = db.Column(db.String(1000))
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
chat = db.relationship('ChatHistory', backref='chatresp', lazy='dynamic')
def __repr__(self):
return '<Response_Master: {}>'.format(self.resp)
class ChatHistory(db.Model):
__tablename__ = 'chathistory'
id = db.Column(db.Integer, primary_key=True)
userText = db.Column(db.String(1000), nullable=False)
convID = db.Column(db.Integer, db.ForeignKey('convmaster.id'))
intentID = db.Column(db.Integer, db.ForeignKey('intentmaster.id'), nullable=False)
respID = db.Column(db.Integer, db.ForeignKey('respmaster.id'), nullable=False)
feedback = db.Column(db.String(5))
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
def __repr__(self):
return '<Chat_History: {}>'.format(self.userText)
答案:
对我有用的东西。
在聊天和响应变量中分别在ConvMaster和IntentMaster的一侧添加参数 foreign_keys 。在这种情况下,这是必需的。
删除 nullable = false 约束。这对升级产生了重大影响,这给外键约束带来了错误。
有效的代码在这里
class ConvMaster(db.Model):
"""
Create a page visit master
"""
__tablename__ = 'convmaster'
id = db.Column(db.Integer, primary_key=True)
convName = db.Column(db.String(60), unique = True)
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
resp = db.relationship('RespMaster', foreign_keys='RespMaster.convID', backref='conv', lazy='dynamic')
chat = db.relationship('ChatHistory', foreign_keys='ChatHistory.convID', backref='chatconv', lazy='dynamic')
def __repr__(self):
return '<ConvMaster: {}>'.format(self.convName)
class IntentMaster(db.Model):
"""
Create a page visit master
"""
__tablename__ = 'intentmaster'
id = db.Column(db.Integer, primary_key=True)
intentName = db.Column(db.String(60), unique = True)
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
resp = db.relationship('RespMaster', foreign_keys='RespMaster.intentID', backref='intent', lazy='dynamic')
chat = db.relationship('ChatHistory', foreign_keys='ChatHistory.intentID', backref='chatintent', lazy='dynamic')
def __repr__(self):
return '<Intent_Master: {}>'.format(self.intentName)
class RespMaster(db.Model):
"""
Create a page visit master
"""
__tablename__ = 'respmaster'
id = db.Column(db.Integer, primary_key=True)
convID = db.Column(db.Integer, db.ForeignKey('convmaster.id'))
intentID = db.Column(db.Integer, db.ForeignKey('intentmaster.id'))
resp = db.Column(db.String(1000))
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
chat = db.relationship('ChatHistory', backref='chatresp', lazy='dynamic')
def __repr__(self):
return '<Response_Master: {}>'.format(self.resp)
class ChatHistory(db.Model):
"""
Create a page visit master
"""
__tablename__ = 'chathistory'
id = db.Column(db.Integer, primary_key=True)
userText = db.Column(db.String(1000), nullable=False)
convID = db.Column(db.Integer, db.ForeignKey('convmaster.id'))
intentID = db.Column(db.Integer, db.ForeignKey('intentmaster.id'))
respID = db.Column(db.Integer, db.ForeignKey('respmaster.id'))
feedback = db.Column(db.String(5))
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
def __repr__(self):
return '<Chat_History: {}>'.format(self.userText)