On Dec 5, 2008, at 3:01 PM, Faheem Mitha wrote:
> > > Hi, > > I'm using sqla with the following schema (see below). I'm creating a > cell > object implicitly, using the function make_cell and the association > proxy > pattern. > > def make_cell(patient_obj, snp_obj, snpval): > patient_obj.snps[snp_obj] = snpval > return patient_obj > > My question is, is there some way to get my hands on the Cell object > that > was just created? If possible, I'd like make_cell to return the cell > object. My immediate reason is that this would make it easy to save > the > object using session.save() (there might be some indirect way to do > this, > of course), but it would be nice anyway. there's no need to session.save() any objects created within associationproxy - when the parent object is saved, or if the parent is already saved, any attachments are also saved using the on-by- default "save-update" cascade. > > > Thanks in advance. Please CC me on any reply. > Regards, > Faheem. > > ****************************************************************** > dbschema.py > ****************************************************************** > from sqlalchemy import * > from sqlalchemy.orm import * > from sqlalchemy.ext.associationproxy import association_proxy > from sqlalchemy.orm.collections import attribute_mapped_collection > from datetime import datetime > > metadata = MetaData('sqlite:///btsnp.sqlite') > > # *patients* > # patient_id (PK) > # (Can use actual patient id as unique/alternate identifier > # Create index). > # sex -> list of choices allowed > # age -> (0, 140) > # time of death > > patient_table = Table( > 'patient', metadata, > Column('id', String(20), primary_key=True, index=True), > Column('celfilename', String(30), nullable=False, index=True, > unique=True), > Column('sex', String(1)), > ) > > cell_table = Table( > 'cell', metadata, > Column('patient_id', None, ForeignKey('patient.id', > onupdate='CASCADE', ondelete='CASCADE'), index=True, nullable=False, > primary_key=True), > Column('snp_id', None, ForeignKey('snp.rsid', > onupdate='CASCADE', ondelete='CASCADE'), index=True, > nullable=False, primary_key=True), > Column('snpval_id', None, ForeignKey('snpval.val', > onupdate='CASCADE', ondelete='CASCADE'), index=True, nullable=False) > ) > > # *snps* > # snp_id (PK) > # name (name of snp) > > snp_table = Table( > 'snp', metadata, > Column('rsid', String(20), nullable=False, primary_key=True), > Column('chromosome', Integer, nullable=False), > Column('location', Integer, nullable=False), > Column('probe_set_id', String(20), nullable=False, unique=True), > Column('allele', String(3), nullable=False), > ) > > # *doublets* > # doublet_id (PK) > # seq (two letters AA, AG) > > snpval_table = Table( > 'snpval', metadata, > Column('val', Integer, primary_key=True), > ) > > metadata.create_all() > > def create_cell(snp, snpval): > return Cell(snp=snp, snpval=snpval) > > class Patient(object): > def __init__(self, id, celfilename, sex): > self.id = id > self.celfilename = celfilename > self.sex = sex > def __repr__(self): > return '<Patient %s>'%self.id > snps = association_proxy('by_rsid', 'snpval', creator=create_cell) > > class Cell(object): > def __init__(self, patient=None, snp=None, snpval=None): > self.patient = patient > self.snp = snp > self.snpval = snpval > def __repr__(self): > return '<Cell %s>'%self.snpval > > class Snp(object): > def __init__(self, rsid, chromosome, location, probe_set_id, > allele): > self.rsid = rsid > self.chromosome = chromosome > self.location = location > self.probe_set_id = probe_set_id > self.allele = allele > def __repr__(self): > return '<SNP %s>'%self.rsid > patients = association_proxy('by_patient', 'snpval', > creator=create_cell) > > class Snpval(object): > def __init__(self, val): > self.val = val > def __repr__(self): > return '<Snpval %s>'%self.val > > # mapper(Broker, brokers_table, properties={ > # 'by_stock': relation(Holding, > # collection_class=attribute_mapped_collection('stock')) > # }) > > # 'cells' corresponds to a 1 to many relation. > mapper(Patient, patient_table, properties={'cells':relation(Cell, > backref='patient'), > 'by_rsid': relation(Cell, > collection_class=attribute_mapped_collection('snp'))} > ) > # 'patient_snpval' corresponds to a many to 1 relation. > # 'patient_snpval' corresponds to a 1 to 1 relation. > mapper(Cell, cell_table, properties={'snp':relation(Snp, > backref='cells'), > 'snpval':cell_table.c.snpval_id, > 'snpval_obj':relation(Snpval, > uselist=False, backref='cell')}) > mapper(Snp, snp_table, properties={'by_patient': relation(Cell, > collection_class=attribute_mapped_collection('patient'))}) > mapper(Snpval, snpval_table) > > #print patient_mapper.identity_key_from_instance() > > # 0) Create doublet (2 letters). > # 1) Enter row names (cols) and patient names( snp ids). > # 2) Look at text files and update linker tables. > > ************************************************************************** > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---