SQLAlchemy报错columns with foreign keys to other colunmns must be declared as @declared_attr

SQLAlchemy_ORM_类继承(抽象类)

https://blog.csdn.net/m0_37714245/article/details/87911920

看了这篇文章,准备自己创建一个基类,不同的是有外键user

class TimeBaseModel(object):

  """模型基类,为模型补充创建时间与更新时间"""

 

  create_time = Column(DateTime, nullable=False, default=func.now())

  create_author_id=外键

  # 记录的创建时间

  update_time = Column(DateTime, nullable=False, default=func.now(), onupdate=func.now()) # 记录的更新时间

提交后,报错columns with foreign keys to other colunmns must be declared as @declared_attr

 

 经过搜索看到这篇文章

https://bbs.csdn.net/topics/394360889?list=67195260

于是用@declared_attr解决了

 

原因是

每个模型必须具有唯一的orm属性.如果mixin中的相同属性直接应用于每个子类,则它们都具有相同的属性.基本orm属性的副本很容易创建,因为它们不引用任何其他orm属性.对于更复杂的属性,使用@declared_attr修饰的函数可确保为每个子类创建新实例.
在检测期间,SQLAlchemy为每个类调用每个声明的attr,并将结果分配给目标名称.通过这种方式,它可以确保为每个子类唯一且正确地进行复杂映射.
见documentation.

 

posted @ 2021-01-15 17:31  ata0083  阅读(241)  评论(0)    收藏  举报