On Thu, Mar 27, 2014 at 1:27 PM, Darin Gordon <[email protected]> wrote:
> Let me preface this by saying that I researched to the best of my abilities
> the answers to my following inquiries.  I would not ask for help without
> first trying to investigate this on my own.  Asking through this forum is
> the proverbial end of the line, so thanks for taking any time to read and
> respond.
>
> I understand relational databases and have been bridging a knowledge gap
> with ORMs, using SQLAlchemy.  I've struggled a bit while learning what
> exactly is going on in the ORM when ADDING new associations in a M to N
> relationship.
>
> My questions are related to SQL Alchemy ORM concepts:
> 1) Relationships
> 2) Association Object pattern
> 3) Association Proxy
>
>
> To help me grasp the concepts, I pieced together a working example of an M
> to N relation and drew a diagram showing the relationships.   The scenario
> is of a very crude appointment scheduler that someone like a realtor may use
> to keep track of who is visiting what property.  My attribute naming
> convention was chosen to facilitate our discussion.  Link:   Github repo
>
> There are two files of interest:
>
> orm_associations.py -- a working example
> "SQLAlchemy - Association Object Relations.pdf" --  contains a diagram of
> the M to N relationship in orm_associations.py
>
>
> In this example, a user can be added to a meeting OR a meeting can be added
> to a user using either of these association proxies:
>
> Meeting.attendees_proxy.append(User)
> or
> User.meetings_proxy.append(Meeting)
>
> These statements create the desired outcome-- a new Appointment record
> inserted into the association table and possibly a new user or meeting
> record if none already exist.
>
> I'd like to know what exactly is happening in the ORM when either of these
> statements is executed.
> For instance, what is the association_proxy creator doing?  If I add a user
> to a meeting, the attendees_proxy creator is defined as follows:
> creator=lambda user: Appointment(appointment_user_relation=user))
> The creator expression appears to be creating a new Appointment object, but
> then defines the RELATIONSHIP as a User object?  I'm missing something here.
> No idea what is going on. It seems to be all a bit to automagical to me.
>
>

The association proxy is purely a shorthand to allow you to get from
Meeting to User and back again without having to worry about the
Appointment object in between. If you didn't have the association
proxy, you would have to do something like this to add a user to a
meeting:

def add_user_to_meeting(user, meeting):
    appointment = Appointment(appointment_user_relation=user,
meeting_appointment_relation=meeting)
    return appointment

The association proxy hides the Appointment object, making it look
like Meetings just have a list of Users:

def add_user_to_meeting(user, meeting):
    meeting.attendees_proxy.append(user)
    # or, alternatively
    # user.meetings_proxy.append(meeting)

When you call "append", it calls your creator() function, passing the
user. Your creator function creates the Appointment object and sets up
the "appointment -> user" side of the relationship. The association
proxy sets up the "appointment -> meeting" side of the relationship
itself, something like this:

    # creator function does this:
    appointment = Appointment(appointment_user_relation=user)
    # Association proxy does this:
    appointment.meeting_appointment_relation = meeting

Hope that helps,

Simon

-- 
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.

Reply via email to