简体   繁体   English

SQLalchemy以声明方式描述关联对象的正确方法是什么

[英]What's the proper way to describe an associative object by SQLalchemy the declarative way

I'm looking for a way to describe an associative object the declarative way. 我正在寻找一种以声明方式描述关联对象的方法。 Beyond storing the foreign keys in the association table, I need to store information like the creation date of the association. 除了将外键存储在关联表中之外,我还需要存储诸如关联创建日期之类的信息。

Today, my model looks like that : 今天,我的模型如下所示:

# Define the User class
class User(Base):
    __tablename__ = 'users'

    # Define User fields
    id = schema.Column(types.Integer(unsigned=True),
        schema.Sequence('users_seq_id', optional=True), primary_key=True)
    password = schema.Column(types.Unicode(64), nullable=False)

# Define the UserSubset class
class UserSubset(Base):
    __tablename__ = 'subsets'

    # Define UserSubset fields
    id = schema.Column(types.Integer(unsigned=True),
        schema.Sequence('subsets_seq_id', optional=True), primary_key=True)
    some_short_description = schema.Column(types.Unicode(50), nullable=False)

# Define the subset memberships table
subset_memberships = schema.Table('group_memberships', Base.metadata,
    schema.Column('user_id', types.Integer(unsigned=True), ForeignKey('users.id')),
    schema.Column('subset_id', types.Integer(unsigned=True), ForeignKey('subsets.id')),
    schema.Column('created', types.DateTime(), default=now, nullable=False),
)

Can I connect everything in an associative object ? 我可以将所有内容连接到关联对象中吗? Or should I change stop using the declarative way ? 还是应该使用声明性方式更改停止?

What you are using at the moment is just a Many-to-Many-relation. 您目前使用的只是多对多关系。 How to work with association objects is described in the docs . 文档中描述了如何使用关联对象。

There is also an extension called associationproxy which simplifies the relation. 还有一个名为associationproxy的扩展,可以简化关系。

As you can see in the manual , configuring a one to many relation is really simple: 如您在手册中所见,配置一对多关系非常简单:

class User(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True)
    name = Column(String(50))
    addresses = relation("Address", backref="user")

class Address(Base):
    __tablename__ = 'addresses'

    id = Column(Integer, primary_key=True)
    email = Column(String(50))
    user_id = Column(Integer, ForeignKey('users.id'))

Many to many relations isn't much harder: 多对多关系并不难:

There's nothing special about many-to-many with declarative. 声明式多对多没有什么特别的。 The secondary argument to relation() still requires a Table object, not a declarative class. Relation()的第二个参数仍然需要Table对象,而不是声明性类。 The Table should share the same MetaData object used by the declarative base: 该表应共享声明基使用的同一MetaData对象:

  keywords = Table('keywords', Base.metadata, Column('author_id', Integer, ForeignKey('authors.id')), Column('keyword_id', Integer, ForeignKey('keywords.id')) ) class Author(Base): __tablename__ = 'authors' id = Column(Integer, primary_key=True) keywords = relation("Keyword", secondary=keywords) 

You should generally not map a class and also specify its table in a many-to-many relation, since the ORM may issue duplicate INSERT and DELETE statements. 一般情况下不应映射一类,还可以指定其表的许多一对多的关系,因为ORM可以发出重复的INSERT和DELETE语句。

Anyway, what you seem to be doing might be better served with inheritance . 无论如何, 继承似乎可以更好地服务您似乎正在做的事情。 Of course, there can be complex table relations that will be a pathological case for the declarative way, but this doesn't seem to be one of them. 当然,可能存在复杂的表关系,这对于声明性方法来说将是一个病态案例,但这似乎不是其中之一。

One more thing, code comments should state what the following code does ans why, not how it does it. 还有一件事,代码注释应说明以下代码的作用以及原因,而不是原因。 Having a # Define the User class comment is almost like having a line of code saying a = 1 # assing value 1 to variable "a" . # Define the User class注释几乎就像有一行代码说a = 1 # assing value 1 to variable "a"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 SQLAlchemy 会话与 Celery 一起使用的正确方法是什么? - What's the proper way to use SQLAlchemy Sessions with Celery? 在 SQLAlchemy (Python) 中联合查询列表的正确方法是什么? - What's the proper way to UNION a list of queries in SQLAlchemy (Python)? 在 Flask SQLALchemy 模型中处理业务逻辑的正确方法是什么? - What's the proper way of handling business logic in Flask SQLALchemy models? 在SQLAlchemy中使用外键插入对象的正确方法是什么? - What is the proper way to insert an object with a foreign key in SQLAlchemy? SQLAlchemy:使用声明式更新的更好方法? - SQLAlchemy: a better way for update with declarative? 使用SQLAlchemy为这种继承建模的正确方法是什么? - What is the proper way to model this type of inheritance with SQLAlchemy? 当验证涉及多个字段时,使用SqlAlchemy验证对象的正确方法是什么? - What is the proper way to validate an object with SqlAlchemy when the validation involves several fields? 如何使用SQlAlchemy声明方式插入数据 - How to insert data with SQlAlchemy declarative way 测试抛出IntegrityError的SQLAlchemy代码的正确方法是什么? - What is a proper way to test SQLAlchemy code that throw IntegrityError? 使用SQLAlchemy的声明性语法时访问表实例的最佳方法 - Best way to access table instances when using SQLAlchemy's declarative syntax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM