[英]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.