Hi Simon,
(I do things a little different on the import side)
Working example (very minimised):
from sqlalchemy import
Integer,Column,DateTime,Unicode,UnicodeText,Boolean,ForeignKey,Interval
from sqlalchemy.ext.declarative import declarative_base
import sqlalchemy.orm as orm
Base = declarative_base()
class Event(Base):
__tablename__ = "CalendarEvents"
Id = Column(Integer, primary_key=True)
CalendarId = Column(ForeignKey('Calendars.Id'))
class Calendar(Base):
__tablename__ = "Calendars"
Id = Column(Integer, primary_key=True)
UserId = Column(Integer, index=True)
ProdId = Column(Unicode(255))
Version = Column(Unicode(5))
CalScale = Column(Unicode(20))
Method = Column(Unicode(10))
Events = orm.relation(Event,backref='Calendar')#,
cascade="all")
if __name__ == '__main__':
engine = create_engine('sqlite:///')
Base.metadata.create_all(bind=engine)
Session = orm.sessionmaker(bind=engine)()
cal = Calendar()
when I run this I get:
Traceback (most recent call last):
File "/var/www/PyWebOs/caltst.py", line 28, in <module>
cal = Calendar()
File "<string>", line 4, in __init__
File "/usr/local/lib/python2.6/site-packages/sqlalchemy/orm/
state.py", line 71, in initialize_instance
fn(self, instance, args, kwargs)
File "/usr/local/lib/python2.6/site-packages/sqlalchemy/orm/
mapper.py", line 1810, in _event_on_init
instrumenting_mapper.compile()
File "/usr/local/lib/python2.6/site-packages/sqlalchemy/orm/
mapper.py", line 666, in compile
"Message was: %s" % mapper._compile_failed)
InvalidRequestError: One or more mappers failed to compile. Exception
was probably suppressed within a hasattr() call. Message was: One or
more mappers failed to compile. Exception was probably suppressed
within a hasattr() call. Message was: One or more mappers failed to
compile. Exception was probably suppressed within a hasattr() call.
Message was: Could not find table 'Calendar' with which to generate a
foreign key
which is excacly the same as I got.....
I have done so mutch in python/sqlalchemy that I feel extremely stupid
not to get this working, it might be just a case of "overreading" the
problem
Martijn
On Oct 15, 2009, at 4:10 PM, King Simon-NFHD78 wrote:
>
>> -----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
-~----------~----~----~----~------~----~------~--~---