your all-on-one relationship appears to "work" because you spelled the name "properties" wrong.
below is the working version of your program. note that specifiying "foreignkey" to mapper is exactly when there is no "real" foreignkey. the "foreignkey" argument is a "directional indicator" that just points to any column in the join condition that represents the "many" side of the relationship.
from sqlalchemy import * engine = create_engine('sqlite://filename=:memory:', echo=True)
job_table = Table('job', engine, Column('id', Integer, primary_key=True), Column('log_ndc_id', Integer), Column('name', Unicode(50))) job_table.create()
event_table = Table('event', engine, Column('not_realy_pk', Integer, primary_key=True), Column('ndc_id', Integer), Column('message', Unicode(2000))) event_table.create()
class Job(object): pass class Event(object): pass Event.mapper = mapper(Event, event_table)
Job.mapper = mapper(Job, job_table, properties = dict( events = relation( Event.mapper, primaryjoin = job_table.c.log_ndc_id==event_table.c.ndc_id, foreignkey=event_table.c.ndc_id)))
j = Job() j.log_ndc_id = 5 j.events.append(Event()) j.events.append(Event()) objectstore.commit()
objectstore.clear() j = Job.mapper.selectfirst() print j.id print len(j.events)
On Mar 14, 2006, at 12:49 PM, Qvx 3000 wrote: I didn't use foreign key because it really isn't a FK. Many other objects could contain the same kind of tag which is then used to extract the messages. Think of it like you have a set of messages and you can group them into subsets using this tag. One subset belongs to a job instance (my example). The other subset belongs to some other procedure which used logging facility to leave a trace of its execution (each message was tagged with id which is unique to some specific invocation of the function). And so on. So potentially, I have a case where LogEvent.NDC_ID points to dozen of tables and in some cases to no table at all.
I guess my problem was because I tried to use add_property. I used add_property because using ActiveMapper doesn't allow me to construct mapper the way I can manually, or I don't know how. Consider this example:
from sqlalchemy import * engine = create_engine('sqlite://filename=:memory:', echo=True)
job_table = Table('job', engine, Column('id', Integer, primary_key=True), Column('log_ndc_id', Integer), Column('name', Unicode(50))) job_table.create()
event_table = Table('event', engine, Column('not_realy_pk', Integer, primary_key=True), Column('ndc_id', Integer), Column('message', Unicode(2000))) event_table.create()
class Job(object): pass class Event(object): pass Event.mapper = mapper(Event, event_table)
# This works: Job.mapper = mapper(Job, job_table, propertied = dict( events = relation( Event.mapper, primaryjoin = job_table.c.log_ndc_id==event_table.c.ndc_id)))
# But the following doesn't work: Job.mapper = mapper(Job, job_table) Job.mapper.add_property('events_late', relation(Event.mapper, primaryjoin = job_table.c.log_ndc_id==event_table.c.ndc_id))
ArgumentError: cant determine primary foreign key in the join relationship....specify foreignkey=<column> or foreignkey=[<columns>]
On 3/14/06, Michael Bayer <[EMAIL PROTECTED]> wrote: why dont you specify the "foreignkey" argument ? just point it to one of the columns involved in the join which represents the "other" side of the relation (assuming the ActiveMapper lets all those arguments come in...). as usual, more specific answers come with full examples :) .
On Mar 14, 2006, at 7:32 AM, Qvx 3000 wrote:
> I'm trying to establish a "relation" between two tables which have > no PK/FK relation in reality. There is a LOG_EVENTS table with > events comming from all arount the system. I can extract usefull > log entries by the use of NDC (nested diagnostic context) which is > really just a tag attached to number of log events. Table JOBS has > a column LOG_NDC_ID which has the same tag. I'm trying to use those > two columns to establish a relationship but I'm getting: > > ========= > sqlalchemy\mapping\properties.py in _find_dependent(self) > 261 self.primaryjoin.accept_visitor(visitor) > 262 if dependent[0] is None: > --> 263 raise ArgumentError("cant determine primary > foreign key in the join relationship....specify foreignkey=<column> > or foreignkey=[<columns>]") > 264 else: > 265 self.foreigntable = dependent[0] > > ArgumentError: cant determine primary foreign key in the join > relationship....specify foreignkey=<column> or foreignkey=[<columns>] > > Here is my attempt: > ========= > class Job(ActiveMapper): > class mapping: > __table__ = 'jobs' > id = column(Integer, primary_key = True) > ... > description = column(Unicode(250), default=None) > #log_events = one_to_many("LogEvent") # Gave up on this one > > class LogEvent(ActiveMapper): > class mapping: > __table__ = 'log_events' > sort = column(Integer, primary_key = True) # not realy a > primary key, used on rare occassions for sorting > ... > ndc_id = column(Integer) # Nested Diagnostic Context > message = column(Unicode(4000)) > > Job.mapper.add_property ("log_events", > relation(LogEvent.mapper, primaryjoin= > LogEvent.c.ndc_id==Job.c.log_ndc_id, lazy=True)) > > Additionally, I was later hoping to extract the messages using > LIMIT/OFFSET, but also have no idea how. > If it can't work like this, I guess I'm just going to make plain > Python property or utility method. > > Thanks, > Tvrtko
------------------------------------------------------- This SF.Net email is sponsored by xPML, a groundbreaking scripting language that extends applications into web and mobile media. Attend the live webcast and join the prime developer group breaking into this new coding territory! http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642 _______________________________________________ Sqlalchemy-users mailing list Sqlalchemy-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users
|