sqlalchemy noForeignKeys错误与关联类和关系

时间:2015-12-04 21:03:06

标签: python sqlalchemy

我的sqlalchemy关系出错了。此关系被建模为关联对象。编译后,我收到以下错误:

"specify a 'primaryjoin' expression." % self.prop) sqlalchemy.exc.NoForeignKeysError: 
     

无法确定加入条件   在关系上的租金/子表之间   Entries.users_who_have_read_this - 没有外键链接   这些表格。确保引用列与a关联   ForeignKey或ForeignKeyConstraint,或指定' primaryjoin' expre   裂变。

class UsersReadArticles(alchemyDB.Model):
    '''
    '''
    __tablename__ = 'users_read_articles'
    id = alchemyDB.Column(alchemyDB.Integer, primary_key = True)
    user_id = alchemyDB.Column(alchemyDB.Integer, alchemyDB.ForeignKey('users.id'))
    article_id = alchemyDB.Column(alchemyDB.Integer, alchemyDB.ForeignKey('entries.id'))
    date_read = alchemyDB.Column(alchemyDB.DateTime)

class Entries(alchemyDB.Model):
    __tablename__ = 'entries'

users_who_have_read_this = alchemyDB.relationship('UsersReadArticles',
                                                  foreign_keys = [UsersReadArticles.user_id],
                                                  backref = alchemyDB.backref('users_who_have_read_this', lazy = 'joined'),
                                                  lazy = 'dynamic',
                                                  cascade = 'all, delete-orphan')

class User(UserMixin, alchemyDB.Model):
    __tablename__ = 'users'
    id = alchemyDB.Column(alchemyDB.Integer, primary_key = True)
    user_email = alchemyDB.Column(alchemyDB.String(64), unique = True, index = True)

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

首先,您的问题是SqlAlchemy无法在表'users_read_articles'上初始化ForeignKey'instry.id'的目标列,因为表'entries'没有名为'id'的列

其次,在mapper users_who_have_read_t中有一个错误:您不需要在表UsersdadArticles上为列'user_id'定义外键。在这种情况下,仅在UsersReadArticles上设置一个foreing键就足够了。

所以,我建议做一些类似的改动:

class UsersReadArticles(alchemyDB.Model):
    __tablename__ = 'users_read_articles'
    id = alchemyDB.Column(alchemyDB.Integer, primary_key = True)
    user_id = alchemyDB.Column(alchemyDB.Integer, alchemyDB.ForeignKey('users.id'))
    article_id = alchemyDB.Column(alchemyDB.Integer, alchemyDB.ForeignKey('entries.id'))
    date_read = alchemyDB.Column(alchemyDB.DateTime, default=datetime.datetime.utcnow)
    user = alchemyDB.relationship("User", backref="usersreadarticles") 

class Entries(alchemyDB.Model):
    __tablename__ = 'entries'
    id = alchemyDB.Column(alchemyDB.Integer, primary_key = True)
    title = alchemyDB.Column(alchemyDB.String(255))
    users_who_have_read_this = alchemyDB.relationship('UsersReadArticles',
                                                  backref = alchemyDB.backref('users_who_have_read_this', lazy = 'joined'),
                                                  lazy = 'dynamic',
                                                  cascade = 'all, delete-orphan')

class User(alchemyDB.Model):
    __tablename__ = 'users'
    id = alchemyDB.Column(alchemyDB.Integer, primary_key = True)
    user_email = alchemyDB.Column(alchemyDB.String(64), unique = True, index = True)

测试:

e = Entries()
e.title = 'Article 1'
alchemyDB.session.add(e)

u = User()
u.user_email = 'dedeco@gmail.com'
alchemyDB.session.add(u)
alchemyDB.session.commit()

r = UsersReadArticles()
r.user_id = u.id
r.article_id = e.id
alchemyDB.session.add(r)
alchemyDB.session.commit()

u = User()
u.user_email = 'xpto@gmail.com'
alchemyDB.session.add(u)
alchemyDB.session.commit()

r = UsersReadArticles()
r.user_id = u.id
r.article_id = e.id
alchemyDB.session.add(r)
alchemyDB.session.commit()

结果:

>>> art = alchemyDB.session.query(Entries).all()
>>> for a in art:
...     print a.title, "Who read:"
...     for u in a.users_who_have_read_this:
...         print u.user.user_email
... 
Article 1 Who read:
dedeco@gmail.com
xpto@gmail.com