I'm sorry, forgot to include Message:
class Message(Base):
__tablename__ = "messages"
id = Column(Integer, primary_key=True)
type = Column(String)
sender_id = Column(Integer, ForeignKey("users.id"))
recipient_association_id = Column(Integer,
ForeignKey(RecipientAssociation.id))
recipient_association = relationship(RecipientAssociation,
backref=backref("message", uselist=False))
recipient = association_proxy("recipient_association", "recipient")
text = Column(String)
delivered = Column(DateTime)
read = Column(DateTime)
media_association_id = Column(Integer, ForeignKey(MediaAssociation.id))
media_association = relationship(MediaAssociation,
backref=backref("message", uselist=False))
media = association_proxy("media_association", "media_object",
creator=MediaAssociation.creator("message"))
(wow, fancy darcula formatting)
Am Sonntag, 5. Juli 2015 17:26:34 UTC+2 schrieb Michael Bayer:
>
>
>
> On 7/4/15 3:56 PM, Fridtjof Mund wrote:
>
> So far I have set up the following:
>
> class User(Base):
> __tablename__ = 'users'
> id = Column(Integer, primary_key=True)
>
> sent_messages = relationship("Message", backref="sender")
> received_messages_association = relationship(RecipientAssociation,
> backref="user")
> received_messages = association_proxy("received_messages_association",
> "message",
>
> creator=MediaAssociation.creator("user"))
> class Group(Base):
> __tablename__ = "groups"
> id = Column(Integer, primary_key=True)
>
> received_messages =
> association_proxy("received_messages_association", "message",
>
> creator=MediaAssociation.creator("group"))
> class RecipientAssociation(Base):
> __tablename__ = "recipient_associations"
> id = Column(Integer, primary_key=True)
>
> @classmethod
> def creator(cls, discriminator):
> """Provide a 'creator' function to use with
> the association proxy."""
>
> return lambda recipient: RecipientAssociation(
> recipient=recipient,
> discriminator=discriminator)
>
> discriminator = Column(String)
> """Refers to the type of recipient."""
>
> @property
> def recipient(self):
> """Return the recipient object."""
> return getattr(self, self.discriminator)
>
> This works fine for a one-to-one i have in the same project (user/group
> with a picture, with uselist=False on backref), and it's from an article
> zzzeek wrote on his blog. See the original source here
> <http://techspot.zzzeek.org/files/2007/discriminator_on_association.py>.
>
> Is there a better way to do this at all, and if there is not, what is
> missing here for this to work? Am I completely thinking in the wrong
> direction?
>
> this is only a fragment (what's "Message"? What are the subtypes we're
> dealing with?) so I don't really know what the goal is, that said the 2007
> article was converted to an updated and enhanced series of examples at
> http://docs.sqlalchemy.org/en/rel_1_0/orm/examples.html#module-examples.generic_associations.
>
>
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected] <javascript:>.
> To post to this group, send email to [email protected]
> <javascript:>.
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.
>
>
>
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.