> -----Original Message-----
> From: [email protected] 
> [mailto:[email protected]] On Behalf Of Martijn Moeling
> Sent: 15 October 2009 14:42
> To: [email protected]
> Subject: [sqlalchemy] Declerative Relation trouble
> 
> Hi All,
> 
> 
> I am having a very bad day (or two to be honest), spending 
> time reading error messages.
> 
> 
> I am sorry to say but the SQLALCHEMY documentation is not very helpful
> when using declarative_base.... when it is about relations......
> 

Without a runnable example which actually shows your problem, it's very
difficult to debug. Here's something I cobbled together based on your
description. It may not be exactly right, but it seems to work:


import sqlalchemy as sa
import sqlalchemy.orm as orm
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class XProp(Base):
    __tablename__  = "CalendarXProps"
    Id = sa.Column(sa.Integer, primary_key=True)
    EventId = sa.Column(sa.ForeignKey('CalendarEvents.Id'))
    AlarmId = sa.Column(sa.ForeignKey('CalendarAlarms.Id'))
    CalendarId = sa.Column(sa.ForeignKey('Calendars.Id'))
    Name = sa.Column(sa.String(20))
    Value = sa.Column(sa.String(20))

class Event(Base):
    __tablename__ = "CalendarEvents"
    Id = sa.Column(sa.Integer, primary_key=True)
    CalendarId = sa.Column(sa.ForeignKey('Calendars.Id'))
    XProps = orm.relation(XProp, backref='Events')

class Alarm(Base):
    __tablename__ = "CalendarAlarms"
    Id = sa.Column(sa.Integer, primary_key=True)
    CalendarId = sa.Column(sa.ForeignKey('Calendars.Id'))
    XProps = orm.relation(XProp, backref='Alarms')

class Calendar(Base):
    __tablename__ = "Calendars"
    Id = sa.Column(sa.Integer, primary_key=True)
    Events = orm.relation(Event, backref='Calendar')
    Alarms = orm.relation(Alarm, backref='Calendar')
    XProps = orm.relation(XProp, backref='Calendar')

if __name__ == '__main__':
    engine = sa.create_engine('sqlite:///')
    Base.metadata.create_all(bind=engine)
    Session = orm.sessionmaker(bind=engine)()

    cal = Calendar()
    cal.Events.append(Event(XProps=[XProp(Name='Hello', Value='World'),
                                    XProp(Name='foo', Value='bar')]))
    

    Session.add(cal)
    Session.flush()

    print cal
    for event in cal.Events:
        print event
        for prop in event.XProps:
            print prop



Hope that helps,

Simon

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