加入验证代码后出现sqlalchemy.exc.NoForeignKeysError错误。
来源:7-1 .管理员登录
慕哥3993229
2017-08-09
跟着这段视频做到第19分钟的时候出错。
在admin/forms加入验证代码(如下)后出错,
def validate_account(self,field):
account = field.data
admin = Admin.query.filter_by(name=account).count()
if admin:
raise ValidationError('用户不存在!')
出错为:
sqlalchemy.exc.NoForeignKeysError
sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between parent/child tables on relationship User.comments - there are no foreign keys linking these tables. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.
尝试了重新创建数据库还是不行。
3回答
-
# 管理员登录日志 class Adminlog(db.Model): __tablename__ = "adminlog" id = db.Column(db.Integer, primary_key=True) # 编号 admin_id = db.Column(db.Integer, db.ForeignKey('admin.id')) # 所属管理员 ip = db.Column(db.String(100)) # 登录IP addtime = db.Column(db.DateTime, index=True, default=datetime.now) # 登录时间 def __repr__(self): return "<Adminlog %r>" % self.id # 操作日志 class Oplog(db.Model): __tablename__ = "oplog" id = db.Column(db.Integer, primary_key=True) # 编号 admin_id = db.Column(db.Integer, db.ForeignKey('admin.id')) # 所属管理员 ip = db.Column(db.String(100)) # 登录IP reason = db.Column(db.String(600)) # 操作原因 addtime = db.Column(db.DateTime, index=True, default=datetime.now) # 登录时间 def __repr__(self): return "<Oplog %r>" % self.id
亲,你好,这两个模型里面的user.id改为admin.id,重新创建一下数据表,你截图models.py的完整代码老师帮你更正!
052017-08-09 -
rustgopy
2017-08-09
# 会员 class User(db.Model): __tablename__ = "user" id = db.Column(db.Integer, primary_key=True) # 编号 name = db.Column(db.String(100), unique=True) # 昵称 pwd = db.Column(db.String(100)) # 密码 email = db.Column(db.String(100), unique=True) # 邮箱 phone = db.Column(db.String(11), unique=True) # 手机号码 info = db.Column(db.Text) # 个性简介 face = db.Column(db.String(255), unique=True) # 头像 addtime = db.Column(db.DateTime, index=True, default=datetime.now) # 注册时间 uuid = db.Column(db.String(255), unique=True) # 唯一标志符 userlogs = db.relationship('Userlog', backref='user') # 会员日志外键关系关联 comments = db.relationship('Comment', backref='user') # 评论外键关系关联 moviecols = db.relationship('Moviecol', backref='user') # 收藏外键关系关联 def __repr__(self): return "<User {}>".format(self.name) def check_pwd(self, pwd): from werkzeug.security import check_password_hash return check_password_hash(self.pwd, pwd) # 会员登录日志 class Userlog(db.Model): __tablename__ = "userlog" id = db.Column(db.Integer, primary_key=True) # 编号 user_id = db.Column(db.Integer, db.ForeignKey('user.id')) # 所属会员 ip = db.Column(db.String(100)) # 登录IP addtime = db.Column(db.DateTime, index=True, default=datetime.now) # 登录时间 def __repr__(self): return "<Userlog %r>" % self.id # 标签 class Tag(db.Model): __tablename__ = "tag" id = db.Column(db.Integer, primary_key=True) # 编号 name = db.Column(db.String(100), unique=True) # 标题 addtime = db.Column(db.DateTime, index=True, default=datetime.now) # 添加时间 movies = db.relationship("Movie", backref='tag') # 电影外键关系关联 def __repr__(self): return "<Tag %r>" % self.name # 电影 class Movie(db.Model): __tablename__ = "movie" id = db.Column(db.Integer, primary_key=True) # 编号 title = db.Column(db.String(255), unique=True) # 标题 url = db.Column(db.String(255), unique=True) # 地址 info = db.Column(db.Text) # 简介 logo = db.Column(db.String(255), unique=True) # 封面 star = db.Column(db.SmallInteger) # 星级 playnum = db.Column(db.BigInteger) # 播放量 commentnum = db.Column(db.BigInteger) # 评论量 tag_id = db.Column(db.Integer, db.ForeignKey('tag.id')) # 所属标签 area = db.Column(db.String(255)) # 上映地区 release_time = db.Column(db.Date) # 上映时间 length = db.Column(db.String(100)) # 播放时间 addtime = db.Column(db.DateTime, index=True, default=datetime.now) # 添加时间 comments = db.relationship("Comment", backref='movie') # 评论外键关系关联 moviecols = db.relationship("Moviecol", backref='movie') # 收藏外键关系关联 def __repr__(self): return "<Movie %r>" % self.title # 上映预告 class Preview(db.Model): __tablename__ = "preview" id = db.Column(db.Integer, primary_key=True) # 编号 title = db.Column(db.String(255), unique=True) # 标题 logo = db.Column(db.String(255), unique=True) # 封面 addtime = db.Column(db.DateTime, index=True, default=datetime.now) # 添加时间 def __repr__(self): return "<Preview %r>" % self.title # 评论 class Comment(db.Model): __tablename__ = "comment" id = db.Column(db.Integer, primary_key=True) # 编号 content = db.Column(db.Text) # 内容 movie_id = db.Column(db.Integer, db.ForeignKey('movie.id')) # 所属电影 user_id = db.Column(db.Integer, db.ForeignKey('user.id')) # 所属用户 addtime = db.Column(db.DateTime, index=True, default=datetime.now) # 添加时间 def __repr__(self): return "<Comment %r>" % self.id # 电影收藏 class Moviecol(db.Model): __tablename__ = "moviecol" id = db.Column(db.Integer, primary_key=True) # 编号 movie_id = db.Column(db.Integer, db.ForeignKey('movie.id')) # 所属电影 user_id = db.Column(db.Integer, db.ForeignKey('user.id')) # 所属用户 addtime = db.Column(db.DateTime, index=True, default=datetime.now) # 添加时间 def __repr__(self): return "<Moviecol %r>" % self.id # 权限 class Auth(db.Model): #__tablename__ = "auth" id = db.Column(db.Integer, primary_key=True) # 编号 name = db.Column(db.String(100), unique=True) # 名称 url = db.Column(db.String(255), unique=True) # 地址 addtime = db.Column(db.DateTime, index=True, default=datetime.now) # 添加时间 def __repr__(self): return "<Auth %r>" % self.name # 角色 class Role(db.Model): #__tablename__ = "role" id = db.Column(db.Integer, primary_key=True) # 编号 name = db.Column(db.String(100), unique=True) # 名称 auths = db.Column(db.String(600)) # 角色权限列表 addtime = db.Column(db.DateTime, index=True, default=datetime.now) # 添加时间 admins = db.relationship("Admin", backref='role') # 管理员外键关系关联 def __repr__(self): return "<Role %r>" % self.name # 管理员 class Admin(db.Model): __tablename__ = "admin" id = db.Column(db.Integer, primary_key=True) # 编号 name = db.Column(db.String(100), unique=True) # 管理员账号 pwd = db.Column(db.String(100)) # 管理员密码 is_super = db.Column(db.SmallInteger) # 是否为超级管理员,0为超级管理员 role_id = db.Column(db.Integer, db.ForeignKey('role.id')) # 所属角色 addtime = db.Column(db.DateTime, index=True, default=datetime.now) # 添加时间 adminlogs = db.relationship("Adminlog", backref='admin') # 管理员登录日志外键关系关联 oplogs = db.relationship("Oplog", backref='admin') # 管理员操作日志外键关系关联 def __repr__(self): return "<Admin %r>" % self.name def check_pwd(self, pwd): from werkzeug.security import check_password_hash return check_password_hash(self.pwd, pwd) # 管理员登录日志 class Adminlog(db.Model): __tablename__ = "adminlog" id = db.Column(db.Integer, primary_key=True) # 编号 admin_id = db.Column(db.Integer, db.ForeignKey('admin.id')) # 所属管理员 ip = db.Column(db.String(100)) # 登录IP addtime = db.Column(db.DateTime, index=True, default=datetime.now) # 登录时间 def __repr__(self): return "<Adminlog %r>" % self.id # 操作日志 class Oplog(db.Model): __tablename__ = "oplog" id = db.Column(db.Integer, primary_key=True) # 编号 admin_id = db.Column(db.Integer, db.ForeignKey('admin.id')) # 所属管理员 ip = db.Column(db.String(100)) # 登录IP reason = db.Column(db.String(600)) # 操作原因 addtime = db.Column(db.DateTime, index=True, default=datetime.now) # 登录时间 def __repr__(self): return "<Oplog %r>" % self.id
00 -
慕哥3993229
提问者
2017-08-09
# coding:utf8
from datetime import datetime
from app import db
# 会员
class User(db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), unique=True)
pwd = db.Column(db.String(100))
email = db.Column(db.String(100), unique=True)
phone = db.Column(db.String(11), unique=True)
info = db.Column(db.Text)
face = db.Column(db.String(255))
addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)
uuid = db.Column(db.String(255), unique=True)
userlogs = db.relationship('UserLog', backref='user')
comments = db.relationship('Comment', backref='user')
moviecols = db.relationship('MovieCol', backref='user')
def __repr__(self):
return '<User:%r>' % self.name
# 会员日志
class UserLog(db.Model):
__tablename__ = 'userlog'
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
ip = db.Column(db.String(100))
addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)
def __repr__(self):
return '<UserLog:%r>' % self.id
# 标签
class Tag(db.Model):
__tablename__ = 'tag'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), unique=True)
addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)
movies = db.ForeignKey('Movie', backref='tag')
def __repr__(self):
return '<Tag:%r>' % self.name
# 电影
class Movie(db.Model):
__tabalename__ = 'movie'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(255), unique=True)
url = db.Column(db.String(255), unique=True)
info = db.Column(db.Text)
logo = db.Column(db.String(255), unique=True)
star = db.Column(db.SmallInteger)
playnum = db.Column(db.BigInteger)
commentnum = db.Column(db.BigInteger)
tag_id = db.Column(db.Integer, db.ForeignKey('tag.id'))
area = db.Column(db.String(255))
releasetime = db.Column(db.Date)
length = db.Column(db.String(100))
addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)
comments = db.relationship('Comment', backref='movie')
moviecols = db.relationship('MovieCol', backref='movie')
def __repr__(self):
return '<Movie:%r>' % self.title
# 上映预告
class Preview(db.Model):
__tabalename__ = 'preview'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(255), unique=True)
logo = db.Column(db.String(255), unique=True)
addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)
def __repr__(self):
return '<Preview:%r>' % self.title
# 评论
class Comment(db.Model):
__tablename__ = 'comment'
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.Text)
user_id = db.Column(db.Integer, primary_key='user.id')
movie_id = db.Column(db.Integer, primary_key='movie.id')
addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)
def __repr__(self):
return 'Comment:%r' % self.id
# 电影收藏
class MovieCol(db.Model):
__tablename__ = 'moviecol'
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, primary_key='user.id')
movie_id = db.Column(db.Integer, primary_key='movie.id')
addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)
def __repr__(self):
return '<MovieCol:%r>' % self.id
# 权限
class Auth(db.Model):
__tablename__ = 'auth'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), unique=True)
url = db.Column(db.String(255), unique=True)
addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)
def __repr__(self):
return '<Auth:%r>' % self.name
# 角色
class Role(db.Model):
__tablename__ = 'role'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), unique=True)
auths = db.Column(db.String(600))
addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)
admins = db.relationship('Admin', backref='role')
def __repr__(self):
return '<Role:%r>' % self.name
# 管理员
class Admin(db.Model):
__tablename__ = 'admin'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), unique=True)
pwd = db.Column(db.String(100))
issuper = db.Column(db.SmallInteger)
role_id = db.Column(db.Integer, db.ForeignKey('role.id'))
addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)
adminlogs = db.relationship('AdminLog', backref='admin')
oplogs = db.relationship('OpLog', backref='admin')
def __repr__(self):
return '<Admin:%r>' % self.name
# 管理员日志
class AdminLog(db.Model):
__tablename__ = 'adminlog'
id = db.Column(db.Integer, primary_key=True)
admin_id = db.Column(db.Integer, db.ForeignKey('admin.id'))
ip = db.Column(db.String(100))
addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)
def __repr__(self):
return '<AdminLog:%r>' % self.id
# 操作日志
class OpLog(db.Model):
__tablename__ = 'oplog'
id = db.Column(db.Integer, primary_key=True)
admin_id = db.Column(db.Integer, db.ForeignKey('admin.id'))
ip = db.Column(db.String(100))
reason = db.Column(db.String(600))
addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)
def __repr__(self):
return '<OpLog:%r>' % self.id
'''
if __name__ == '__main__':
db.create_all()
'''00
相似问题