Sanjaya Kumar Patel wrote:
> Dear All,
> 
> I am not being able to figure out how relate to a table having composite 
> primary key. To illustrate my problem, I am giving below a schema. Any 
> guidence on how to code this in SQLAlchemy (and in ActiveMapper, if 
> possible) is more than appreciable. I would prefer autoloding Tables, rather 
> that defining all columns in the code.

I don't think autoloading is possible with ActiveMapper (doesn't it 
defeat the point?), but the below demonstrates compound primary and 
foreign keys.

=====================
from sqlalchemy import *
from sqlalchemy.ext.activemapper import *
import sqlalchemy.ext.activemapper as activemapper

activemapper.metadata.connect('sqlite:///:memory:')
activemapper.metadata.engine.echo = True

class Employee(ActiveMapper):
     class mapping:
         __table__ = 'employees'
         company_id = column(Integer, primary_key=True)
         employee_no = column(Integer, primary_key=True)
         name = column(String)
         dependants = one_to_many('Dependant', backref='employee')

class Dependant(ActiveMapper):
     class mapping:
         __table__ = 'dependants'
         company_id = column(Integer, primary_key=True,
                             foreign_key='employees.company_id')
         employee_no = column(Integer, primary_key=True,
                              foreign_key='employees.employee_no')
         dependant_no = column(Integer, primary_key=True)
         name = column(String)

activemapper.create_tables()

e = Employee(company_id=1, employee_no=1, name='Foo Wilson')
e.dependants.append(Dependant(name='Bar Wilson', dependant_no=1))

activemapper.objectstore.flush()
=====================



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