我希望能够为这样的事件添加被邀请者:
e = events.get(1)
u = users.get(1)
e.invitees.append(u)
然后在我的服务层中,我希望能够拥有如下功能:
def add_invitee(self, event, user):
if user in self.get(event).invitees:
raise OroposError(u'User already invited')
event.invitees.append(user)
return self.save(event), user
当我尝试这个时,会发生一些事情。当我尝试使用我的功能时,我收到此错误:
InterfaceError:(InterfaceError)错误绑定参数0 - 可能 不支持的类型。 u'SELECT events.id AS events_id,events.user_id AS events_user_id,events.event_name AS events_event_name, events.event_mesg AS events_event_mesg,events.start_date AS events_start_date,events.end_date AS events_end_date, events.event_status AS events_event_status,events.rsvp AS events_rsvp,events.created AS events_created \ nFROM events \ nWHERE events.id =?' (,)
当我尝试e.invitees.append(u)时,我收到此错误:
File "/Users/f/.virtualenvs/Oropos/lib/python2.7/site-packages/sqlalchemy/orm/collections.py", line 1057, in append
item = __set(self, item, _sa_initiator)
File "/Users/f/.virtualenvs/Oropos/lib/python2.7/site-packages/sqlalchemy/orm/collections.py", line 1029, in __set
item = getattr(executor, 'fire_append_event')(item, _sa_initiator)
File "/Users/f/.virtualenvs/Oropos/lib/python2.7/site-packages/sqlalchemy/orm/collections.py", line 733, in fire_append_event
item, initiator)
File "/Users/f/.virtualenvs/Oropos/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 910, in fire_append_event
value = fn(state, value, initiator or self)
File "/Users/f/.virtualenvs/Oropos/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 1140, in emit_backref_from_collection_append_event
child_impl = child_state.manager[key].impl
KeyError: 'event'
这让我困扰了一段时间。有什么建议?
以下是与我的问题相关的模型。
class EventJsonSerializer(JsonSerializer):
pass
class Event(EventJsonSerializer, db.Model):
__tablename__ = 'events'
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
event_name = db.Column(db.String(30))
event_mesg = db.Column(db.Text)
start_date = db.Column(db.Date)
end_date = db.Column(db.Date)
event_status = db.Column(db.Boolean)
rsvp = db.Column(db.Date)
created = db.Column(db.DateTime, default=get_current_time)
invitees = db.relationship('Invite', backref=db.backref('event'))
proposals = db.relationship('Proposal', backref=db.backref('event'))
class InviteJsonSerializer(JsonSerializer):
pass
class Invite(InviteJsonSerializer, db.Model):
__tablename__ = 'invites'
id = db.Column(db.Integer, primary_key=True)
created = db.Column(db.Date, default=get_current_time)
user_id = db.Column(db.ForeignKey('users.id'))
event_id = db.Column(db.ForeignKey('events.id'))
user = db.relationship('User', foreign_keys='Invite.user_id', lazy='joined')
class ProposalJsonSerializer(JsonSerializer):
pass
class Proposal(ProposalJsonSerializer, db.Model):
__tablename__ = 'proposals'
id = db.Column(db.Integer, primary_key=True)
created = db.Column(db.Date, default=get_current_time)
date = db.Column(db.Date)
user_id = db.Column(db.ForeignKey('users.id'))
event_id = db.Column(db.ForeignKey('events.id'))
答案 0 :(得分:0)
如果我没弄错,你试图在User
个实例列表中添加Invite
个实例,你确定它是对的吗?
根据您的代码,e.invitees
包含Invite
个实例,这些实例的外键event_id
指向Event
实例e
。如果您希望invitees
表示与User
表的多对多关系(由两个外键暗示),您可能需要将其变为association table }。另外,我敢说一个User
只能有一个Invite
到一个Event
,这意味着这些字段应该与UniqueConstraint表参数相关联。 / p>