尝试使用db_create.py构建数据库时出现以下错误。根据文档,当你使用backref时,它应该向另一个类添加一个外键,但由于某种原因它不是。
错误:
sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between parent/child tables on relationship Message.users - there are no foreign keys linking these tables. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.
models.py
class Message(db.Model):
id=db.Column(db.Integer, primary_key=True)
message=db.Column(db.String(69), unique=True, nullable=False)
timestamp=db.Column(db.DateTime)
#Problem Code below
users=db.relationship('Message_User', backref='message', lazy='dynamic')
children=db.relationship('Message_Group', backref='message', lazy='dynamic')
#group backrefs Group
class Message_User(db.Model):
__tablename__='message_user'
id=db.Column(db.Integer, primary_key=True)
acknowledge=db.Column(db.DateTime, default=False)
timestamp=db.Column(db.DateTime)
#group backrefs Message_Group
#message backrefs Message
db_createpy
#!flask/bin/python
from migrate.versioning import api
from config import SQLALCHEMY_DATABASE_URI
from config import SQLALCHEMY_MIGRATE_REPO
from app import db
import os.path
db.create_all()
if not os.path.exists(SQLALCHEMY_MIGRATE_REPO):
api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository')
api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
else:
api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO,
api.version(SQLALCHEMY_MIGRATE_REPO))