SQLAlchemy ManyToMany带有附加字段的辅助表 - python

我有3个表:User,Community,community_members(用于用户和社区的many2many关系)。

我使用Flask-SQLAlchemy创建此表:

community_members = db.Table('community_members',
                db.Column('user_id', db.Integer, db.ForeignKey('user.id')),
                db.Column('community_id', db.Integer, db.ForeignKey('community.id')),
                )


class Community(db.Model):
    __tablename__ = 'community'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False, unique=True)
    members = db.relationship(User, secondary=community_members,
                            backref=db.backref('community_members', lazy='dynamic'))

现在,我想像这样向community_members添加其他字段:

community_members = db.Table('community_members',
                db.Column('id', db.Integer, primary_key=True),
                db.Column('user_id', db.Integer, db.ForeignKey('user.id')),
                db.Column('community_id', db.Integer, db.ForeignKey('community.id')),
                db.Column('time_create', db.DateTime, nullable=False, default=func.now()),
                )

现在,在python shell中,我可以这样做:

创建社区:

> c = Community()
> c.name = 'c1'
> db.session.add(c)
> db.session.commit()

将成员添加到社区:

> u1 = User.query.get(1)
> u2 = User.query.get(2)
> c.members.append(u1)
> c.members.append(u2)
> db.session.commit()

> c.members
[<User 1>, <User 2>]

好的,这可行。

但是,现在如何获取community_members表的time_create?

参考方案

您将不得不从使用简单的多对多关系切换为使用“关联对象”,该方法基本上只是获取关联表并为其提供适当的类映射。然后,您将定义与UserCommunity的一对多关系:

class Membership(db.Model):
    __tablename__ = 'community_members'

    id = db.Column('id', db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    community_id = db.Column(db.Integer, db.ForeignKey('community.id'))
    time_create = db.Column(db.DateTime, nullable=False, default=func.now())

    community = db.relationship(Community, backref="memberships")
    user = db.relationship(User, backref="memberships")


class Community(db.Model):
    __tablename__ = 'community'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False, unique=True)

但是您可能只是偶尔对创建时间感兴趣。您想恢复旧的关系!好吧,您不想设置两次relationship;因为sqlalchemy会以某种方式认为您想要两个关联;这一定意味着不同!您可以通过添加association proxy.

from sqlalchemy.ext.associationproxy import association_proxy

Community.members = association_proxy("memberships", "user")
User.communities = association_proxy("memberships", "community")

sqlalchemy在多列中唯一 - python

假设我有一个代表位置的类。位置“属于”客户。位置由Unicode 10个字符代码标识。对于特定客户,“位置代码”在位置之间应该是唯一的。The two below fields in combination should be unique customer_id = Column(Integer,ForeignKey('customers.cus…

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

SQLAlchemy:避免多重继承并拥有抽象基类 - python

因此,我有一堆使用SQLAlchemy的表,它们被建模为对象,这些对象从结果继承到对declarative_base()的调用。即:Base = declarative_base() class Table1(Base): # __tablename__ & such here class Table2(Base): # __tablename__ …

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

SQLalchemy数据库列应采用哪种模型来包含数据数组? - python

因此,我正在尝试建立一个数据库,该数据库的行将经常修改。例如,每小时,我想向数据库的特定部分添加一个数字。因此,如果将self.checkmarks输入等于3的数据库,最好的方法是用增加的数字更新数据库的此部分以使self.checkmarks现在等于3, 2?我尝试将列建立为db.Array,但出现属性错误: AttributeError:“ SQLAlc…