Hi All,

Summary:
1. Seeking help on how to relate a table to itself (using an association 
table).
2. Facing issue while using more than one foreign keys from table1 pointing 
to table2.

Elaborated:
Consider the requirement - A person can have multiple persons in his contact 
list, and a person can be present in the contact list of multiple persons. 
The postgres schema below clears the scenario:

CREATE TABLE person (
  id SERIAL,
  first_name VARCHAR(30) NOT NULL,
  last_name VARCHAR(30) NOT NULL,
  PRIMARY KEY(id)
);

CREATE TABLE contact (
  person_id INTEGER NOT NULL REFERENCES person,
  contact_id INTEGER NOT NULL REFERENCES person,
  favorite BOOLEAN NOT NULL DEFAULT FALSE,
  PRIMARY KEY(person_id, contact_id)
);

Being new to SQLAlchemy, ActiveMapper and Python, I could not conclude the 
code pattern for this for ActiveMapper. Kindly help.

Forgeting about the many_to_many relationship, I then, for the sake of 
trying, viewed the scenario as a one_to_many relationship between person and 
contact object. That is "a person has many contacts." I got some strange 
results. Presenting the code below:

#Trying to have 'Jonathan LaCour' in 'Sanjay Patel's contact list:
from sqlalchemy.ext.activemapper import *
metadata.connect('postgres://username:[EMAIL PROTECTED]:5432/mydb')

class Person(ActiveMapper):
    class mapping:
        __autoload__ = True
        contacts = one_to_many('Contact', colname='person_id', 
backref='contact_of')
class Contact(ActiveMapper):
    class mapping:
        __autoload__ = True

jonathan = Person(first_name = "Jonathan", last_name = "LaCour");
objectstore.flush()
print jonathan.id          # 1
sanjay = Person(first_name = "Sanjay", last_name = "Patel",
           contacts = [Contact(contact_id=jonathan.id, favorite=True)])
objectstore.flush()
print sanjay.id         # 2
print sanjay.contacts[0].person_id               # 2
print sanjay.contacts[0].contact_id              # 2 (it should be 1!)

Is it a bug, or I am missing something?

Thanks
Sanjay




-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to