【问题标题】:What is the use of __table_args__ = {'extend_existing': True} in SQLAlchemy?SQLAlchemy 中的 __table_args__ = {'extend_existing': True} 有什么用?
【发布时间】:2019-12-29 23:20:14
【问题描述】:

我有一个烧瓶应用程序,我正在尝试将其转换为 Django。在继承抽象基础模型的模型之一中,它被称为

__table_args__ = {'extend_existing': True}

谁能通过一个小例子解释一下这在 SQLAlchemy 中的含义。

我已经阅读了几篇文章,但是当我使用 Django 和刚接触 Flask-SQLAlchemy 时,我无法正确理解它。

https://hackersandslackers.com/manage-database-models-with-flask-sqlalchemy/ https://docs.sqlalchemy.org/en/13/orm/extensions/declarative/table_config.html

【问题讨论】:

标签: python django sqlalchemy


【解决方案1】:

此参数用于确定是否允许该表为其继承类添加新列,或者可以说是“扩展”。

例如,我有:

class User(Base):
  __table_args__ = {'extend_existing': True}
  
  attr1 = Column(String)
  attr2 = Column(Integer)

class ChildUser(User):
  attr3 = Column(Integer) # this will allow this column to 'extend' the parent attriubtes.

此参数默认为“False”,通常您不希望更改此设置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 2020-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多