带有关联类和关系的sqlalchemy noForeignKeys错误

用户名

我的sqlalchemy关系出现错误。该关系被建模为关联对象。编译后,出现以下错误:

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

无法确定关系Entries.users_who_have_read_this上的租用/子级表之间的联接条件-没有链接这些表的外键。确保引用列与ForeignKey或ForeignKeyConstraint关联,或指定“ primaryjoin”表达式。

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)

我在这里做错了什么?

安德烈·阿劳霍(Andre Araujo)

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

其次,在映射器users_who_have_read_this中有一个错误:您不需要为表UsersReadArticles上的“ user_id”列定义外键。在这种情况下,仅在UsersReadArticles上设置一个前导键就足够了。

因此,我建议进行如下更改:

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 = '[email protected]'
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 = '[email protected]'
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:
[email protected]
[email protected]

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

获取具有关联关系的实体时,FOSRestBundle出现HTTP 500错误

来自分类Dev

教义symfony2错误:类厨房\ PurchasesBundle \实体\产品没有关联的名为供应商的关联

来自分类Dev

错误试图在Laravel 5中获取具有关系和范围的非对象的属性

来自分类Dev

在SQLAlchemy类中查找所有关联代理

来自分类Dev

Flask:使用带有关系的SQLalchemy模型填充WTform

来自分类Dev

为什么我会收到带有 sqlalchemy 元数据的现有表的“关系不存在”错误?

来自分类Dev

语义错误行0,列63在g,产品p附近:错误:类AppBundle \ Entity \ Product没有关联symfony2应用程序

来自分类Dev

带有SQLAlchemy编码错误的Python

来自分类Dev

带有关联和多个条件的 Rails where 子句

来自分类Dev

代码优先关系-有关多重性的获取错误

来自分类Dev

原则-[语义错误]-错误:类没有字段或关联

来自分类Dev

Flask-WTForms和带有关系的SQLAalchemy SelectField

来自分类Dev

Rails 关联错误的类

来自分类Dev

SQLAlchemy-如何查询3个表(带有关联表)

来自分类Dev

有关的BufferedWriter类错误java12目录

来自分类Dev

创建带有GUI的矩形类(错误)

来自分类Dev

语义错误:类实体没有命名的字段或关联

来自分类Dev

带有类声明和全局名称“父级”的tkinter GUI错误未定义?

来自分类Dev

内存泄漏和带有可运行类的奇怪错误-如何正确处理对象?

来自分类Dev

带有构造函数 ILogger 和选项 netcore 2.1 vs2017 的 Moq 类出现错误

来自分类Dev

类关系,NullPointerException错误

来自分类Dev

带有关联值的Swift枚举

来自分类Dev

带有关联的FactoryGirl模型规格

来自分类Dev

带有关联值的Swift枚举

来自分类Dev

多对多关联对象和定义的所有关系在删除时崩溃

来自分类Dev

Neo4j / Cypher删除具有关联关系的节点和子节点

来自分类Dev

如何解决“ ..dictionary条目必须具有关联的键”错误?

来自分类Dev

每个字典条目都必须在WPF项目中具有关联的键错误

来自分类Dev

错误:“您的部署没有关联的swagger.json”-流分析作业上的ACI部署

Related 相关文章

  1. 1

    获取具有关联关系的实体时,FOSRestBundle出现HTTP 500错误

  2. 2

    教义symfony2错误:类厨房\ PurchasesBundle \实体\产品没有关联的名为供应商的关联

  3. 3

    错误试图在Laravel 5中获取具有关系和范围的非对象的属性

  4. 4

    在SQLAlchemy类中查找所有关联代理

  5. 5

    Flask:使用带有关系的SQLalchemy模型填充WTform

  6. 6

    为什么我会收到带有 sqlalchemy 元数据的现有表的“关系不存在”错误?

  7. 7

    语义错误行0,列63在g,产品p附近:错误:类AppBundle \ Entity \ Product没有关联symfony2应用程序

  8. 8

    带有SQLAlchemy编码错误的Python

  9. 9

    带有关联和多个条件的 Rails where 子句

  10. 10

    代码优先关系-有关多重性的获取错误

  11. 11

    原则-[语义错误]-错误:类没有字段或关联

  12. 12

    Flask-WTForms和带有关系的SQLAalchemy SelectField

  13. 13

    Rails 关联错误的类

  14. 14

    SQLAlchemy-如何查询3个表(带有关联表)

  15. 15

    有关的BufferedWriter类错误java12目录

  16. 16

    创建带有GUI的矩形类(错误)

  17. 17

    语义错误:类实体没有命名的字段或关联

  18. 18

    带有类声明和全局名称“父级”的tkinter GUI错误未定义?

  19. 19

    内存泄漏和带有可运行类的奇怪错误-如何正确处理对象?

  20. 20

    带有构造函数 ILogger 和选项 netcore 2.1 vs2017 的 Moq 类出现错误

  21. 21

    类关系,NullPointerException错误

  22. 22

    带有关联值的Swift枚举

  23. 23

    带有关联的FactoryGirl模型规格

  24. 24

    带有关联值的Swift枚举

  25. 25

    多对多关联对象和定义的所有关系在删除时崩溃

  26. 26

    Neo4j / Cypher删除具有关联关系的节点和子节点

  27. 27

    如何解决“ ..dictionary条目必须具有关联的键”错误?

  28. 28

    每个字典条目都必须在WPF项目中具有关联的键错误

  29. 29

    错误:“您的部署没有关联的swagger.json”-流分析作业上的ACI部署

热门标签

归档