On Mar 24, 2011, at 5:08 PM, Nickle wrote:

> Doesn't quite work.
> 
> What I think is the problem is that when using secondary on the
> association table, there are two foreign keys both referencing a
> calendar. It doesn't know which to pick.
> 
> union mapper looks like this
> 
> union_calendar_mapper  = \
>    mapper \
>        (
>        UnionCalendar,
>        inherits=calendar_mapper,
>        polymorphic_identity='union',
>        properties =
>            {
>            'children':relationship(Calendar,
> secondary='calendar_children_table')
>            }
>        )

oh thats correct, so "primaryjoin" and "secondaryjoin" clear that up, also you 
pass the Table object itself to "secondary" without the string:

relationship(Calendar, secondary=calendar_children_table, 
                     
primaryjoin=calendar_children_table.c.parent_id=calendars_table.c.id, 
                     
secondaryjoin=calendar_children_table.c.child_id=calendars_table.c.id
)

so it knows UnionCalendar id 12 linked to Calendar id 17, put the "12" in 
"parent_id", put the "17" in "child_id".   Similarly it knows which side is 
which inside a query(UnionCalendar).join("children").

> 
> 
> On Mar 24, 4:29 pm, Michael Bayer <[email protected]> wrote:
>> On Mar 24, 2011, at 4:40 AM, Nickle wrote:
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>>> I'm trying to implement a Composite 
>>> Pattern,http://en.wikipedia.org/wiki/Composite_pattern
>> 
>>> In this case its to represent some holiday calendars. I need to
>>> implement a union calendar that is a collection of other calendars.
>> 
>>> So a straightforward hierarchy. Calendar, abstract as the top level
>>> class. SimpleCalendar that inherits from Calendar. Then a union
>>> calendar that inherits from Calendar, but I need to implement
>>> children, a collection of calendars.
>> 
>>> So its a self referential hierarchy. The point to note is that one
>>> calendar can appear in more than one union calendar, so its a self
>>> referential many to many. Its certain I need an association table as a
>>> result.
>> 
>>> I'm not quite sure how to configure this.
>> 
>>> I'm trying something along these lines
>> 
>>> calendars_table = Table \
>>>    (
>>>    'calendars',
>>>    metadata,
>>>    Column ('id', Integer, primary_key=True),
>>>    Column ('code', String, nullable=False, unique=True),
>>>    Column ('name', String),
>>>    Column ('calendar_type', String (20), nullable=False),
>>>    Column ('monday', Boolean),
>>>    Column ('tuesday', Boolean),
>>>    Column ('wednesday', Boolean),
>>>    Column ('thurday', Boolean),
>>>    Column ('friday', Boolean),
>>>    Column ('saturday', Boolean),
>>>    Column ('sunday', Boolean)
>>>    )
>> 
>>> calendar_children_table = Table \
>>>    (
>>>    'calendar_children',
>>>    metadata,
>>>    Column ('parent_id', Integer, ForeignKey ('calendars.id')),
>>>    Column ('child_id',  Integer, ForeignKey ('calendars.id'))
>>>    )
>> 
>>> calendar_mapper = mapper (Calendar, calendars_table,
>>> polymorphic_on=calendars_table.c.calendar_type,
>>> polymorphic_identity="calendar")
>>> simple_calendar_mapper = mapper (SimpleCalendar,
>>> inherits=calendar_mapper,
>>> polymorphic_identity='simple',properties={'holidays':relationship(Holiday,
>>> backref='calendar')})
>>> union_calendar_mapper  = mapper (UnionCalendar,
>>> inherits=calendar_mapper, polymorphic_identity='union')
>> 
>>> holidays_table = Table \
>>>    (
>>>    'holidays',
>>>    metadata,
>>>    Column ('id',          Integer, primary_key=True),
>>>    Column ('holiday_type',        String(20),  nullable=False),
>>>    Column ('calendar_id', Integer, ForeignKey ('calendars.id')),
>>>    Column ('description', String ),
>>>    Column ('year',        Integer),
>>>    Column ('month',       Integer),
>>>    Column ('day',         Integer)
>>>    )
>> 
>>> holiday_mapper           = mapper (Holiday,          holidays_table,
>>> polymorphic_on=holidays_table.c.holiday_type,
>>> polymorphic_identity='holiday')
>>> repeating_holiday_mapper = mapper (RepeatingHoliday,
>>> inherits=holiday_mapper, polymorphic_identity='repeating')
>>> simple_holiday_mapper    = mapper (SimpleHoliday,
>>> inherits=holiday_mapper, polymorphic_identity='simple')
>> 
>>> However, I'm not sure how to map the relationships with the
>>> association table.
>> 
>>> Points that I think are relevant are
>> 
>>> 1. It's only the UnionCalendar that has children.
>> 
>> apply a relatlonship() to the UnionCalendar mapping, the target class is 
>> Calendar, "secondary" is calendar_children_table.
>> 
>>> 2. Deleting a UnionCalendar should delete the entries in the
>>> association table, but not the children calendars.
>> 
>> This is a given when using "secondary" with relationship.   Severing the 
>> link between parent and child corresponds to a deletion of the row in 
>> "secondary".
>> 
>>> 3. Navigation is really only needed from the parent UnionCalendar to
>>> the underlying children calendars. However, having a collection back
>>> the other way might have some uses.
>> 
>> just add "backref='parent_containers'" or similar to the relationship().
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>>> 4. For SimpleCalender to its Holidays, I haven't yet implemented the
>>> cascaded delete, but I know how to do this.
>> 
>>> Thanks
>> 
>>> Nick
>> 
>>> --
>>> You received this message because you are subscribed to the Google Groups 
>>> "sqlalchemy" group.
>>> To post to this group, send email to [email protected].
>>> To unsubscribe from this group, send email to 
>>> [email protected].
>>> For more options, visit this group 
>>> athttp://groups.google.com/group/sqlalchemy?hl=en.
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to