I have two tables Incident and Entry with a 1:many relationship.
Incident.orr_id is a primary key.  Entry.entry_id is a primary key,
and Entry.orr_id is a foreign key.  (The column names are a legacy
tradition.)  I have the following model and classes:

t_incident = Table("Incident", meta,
    autoload=True, autoload_with=engine)

t_entry = Table("Entry", meta,
    Column('orr_id', types.Integer, ForeignKey(t_incident.c.orr_id)),
    autoload=True, autoload_with=engine)

class Incident(object):
    pass

class Entry(object):
    @classmethod
    def get(class_, entry_id):
        return Session.query(class_).get(entry_id)

mapper(Entry, t_entry)

mapper(Incident, t_incident, properties={
    'entries': relation(Entry, backref="incident"),
    })

If I run "x = Entry.get(519010)", I get an exception:

<class 'sqlalchemy.exceptions.ArgumentError'>: Error determining
primary and/or secondary join for relationship 'Incident.entries
(Entry)'. If the underlying error cannot be corrected, you should
specify the 'primaryjoin' (and 'secondaryjoin', if there is an
association table present) keyword arguments to the relation()
function (or for backrefs, by specifying the backref using the
backref() function with keyword arguments) to explicitly specify the
join conditions. Nested error is "Can't find any foreign key
relationships between 'Incident' and 'Entry'"

However, if I run the same statement again, it works.

>>> x = Entry.get(519010)
>>> x.entry_id
519010L
>>> x.orr_id = 7704L
>>> x.incident
<type 'exceptions.AttributeError'>: 'Entry' object has no attribute 'incident'

Oops, the backref doesn't work.  I was also getting an AttributeError
on the 'get' method earlier, though that may have cleared up.

But when I list all the columns rather than autoloading, the problems
seem to go away. (Knock on wood.)



-- 
Mike Orr <[EMAIL PROTECTED]>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
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