我无法确定如何使用以下数据库设置从事件记录中更改TblEventsContacts中的主字段值。
import sqlalchemy as sa
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy import orm
import uuid
Base = declarative_base()
Session = orm.sessionmaker()
Engine = sa.create_engine('sqlite:///arandomdb', echo=True)
Session.configure(bind=Engine)
session = Session()
class TblContactsContacts(Base):
__tablename__ = 'tbl_contacts_contacts'
parent_id = sa.Column(sa.String, sa.ForeignKey('tbl_contacts.guid'), primary_key=True)
child_id = sa.Column(sa.String, sa.ForeignKey('tbl_contacts.guid'), primary_key=True)
class TblEventsEvents(Base):
__tablename__ = 'tbl_events_events'
parent_id = sa.Column(sa.String, sa.ForeignKey('tbl_events.guid'), primary_key=True)
child_id = sa.Column(sa.String, sa.ForeignKey('tbl_events.guid'), primary_key=True)
class TblEvents(Base):
__tablename__ = 'tbl_events'
guid = sa.Column(sa.String(36), primary_key=True, nullable=False)
name = sa.Column(sa.String)
children = orm.relationship('TblEvents',
secondary='tbl_events_events',
primaryjoin='TblEvents.guid==TblEventsEvents.parent_id',
secondaryjoin='TblEvents.guid==TblEventsEvents.child_id',
)
contacts = orm.relationship('TblContacts', secondary='tbl_events_contacts', backref='events')
class TblContacts(Base):
__tablename__ = 'tbl_contacts'
guid = sa.Column(sa.String(36), primary_key=True, nullable=False)
name = sa.Column('contact', sa.String)
children = orm.relationship('TblContacts',
secondary='tbl_contacts_contacts',
primaryjoin='TblContacts.guid==TblContactsContacts.parent_id',
secondaryjoin='TblContacts.guid==TblContactsContacts.child_id',
)
class TblEventsContacts(Base):
__tablename__ = 'tbl_events_contacts'
event_id = sa.Column(sa.String, sa.ForeignKey(TblEvents.guid), primary_key=True)
contact_id = sa.Column(sa.String, sa.ForeignKey(TblContacts.guid), primary_key=True)
primary = sa.Column(sa.Boolean)
event = orm.relationship('TblEvents',
backref=orm.backref('contacts_assoc',
passive_deletes='all'
))
contact = orm.relationship('TblContacts',
backref=orm.backref('events_assoc',
passive_deletes='all'))
if __name__ == '__main__':
Base.metadata.create_all(Engine)
ev1 = TblEvents(guid=str(uuid.uuid4()), name='event 1')
con1 = TblContacts(guid=str(uuid.uuid4()), name='contact 1')
session.add(ev1, con1)
session.commit()
ev1.contacts.append(con1)
session.commit()
我觉得这是我得到的最接近的东西。
ev1.contacts[0].events_assoc.primary = True
session.commit()
我是否必须使用查询,或者在orm中有方法吗?我这样做时不会抱怨,但也不会做任何事情。所以我不确定从这里去哪里。
答案 0 :(得分:0)
哇,我比我想像的要近。最终就是这个。
ev1.contacts[0].events_assoc[0].primary = True
session.commit()
仅在events_assoc之后缺少索引说明符