2010-02-09 2 views
1

Salut ont les tableaux suivants ontsqlalchemy backref lent

nfiletable = Table(
    'NFILE', base.metadata, 
    Column('fileid', Integer, primary_key=True), 
    Column('path', String(300)), 
    Column('filename', String(50)), 
    Column('filesize', Integer), 
    schema='NATIVEFILES')#,autoload=True,autoload_with=engine) 

sheetnames_table=Table(
    'SHEETNAMES', base.metadata, schema='NATIVEFILES', 
    autoload=True, autoload_with=engine) 


nfile_sheet_table=Table(
    'NFILE_SHEETNAME',base.metadata, 
    Column('fileid', Integer, ForeignKey(nfiletable.c.fileid)), 
    Column('sheetid', Integer, ForeignKey(sheetnames_table.c.sheet_id)), 
    schema='NATIVEFILES') 

et cartographes:

nfile_mapper=mapper(Nfile,nfiletable) 

mapper(Sheet, sheetnames_table, properties={ 
    'files': relation(
     Nfile, secondary=nfile_sheet_table, 
     primaryjoin=(sheetnames_table.c.sheet_id==nfile_sheet_table.c.sheetid), 
     secondaryjoin=(nfile_sheet_table.c.fileid==nfiletable.c.fileid), 
     foreign_keys=[nfile_sheet_table.c.sheetid,nfile_sheet_table.c.fileid], 
     backref='sheets') 
}) 

quand je fais ce qui suit

upl = Session.query(Nfile).filter_by(fileid=k).one() 
sheetdb=[] 
for sheet in sheetstoadd: 
    s = sheetcache[sheetname] 
    sheetdb.append(s) 
upl.sheets = sheetdb 
Session.save(upl) 
Session.flush() 

la ligne upl.sheets = sheetdb prend pour toujours. Il semble que tous les fichiers de chaque feuille dans sheetdb sont chargés à partir de la base de données. Comment puis-je empêcher cela?

Répondre

1

si NFile.sheets fait référence à une collection énorme, mettre "paresseux =" dynamique" sur le backref:

mapper(Sheet, sheetnames_table, properties={ 
    'files': relation(
     Nfile, secondary=nfile_sheet_table, 
     backref=backref('sheets', lazy='dynamic')) 
}) 

Tous les trucs primaryjoin/secondaryjoin/de FOREIGN_KEYS est pas non plus nécessaire puisque votre nfile_sheet_table a ForeignKey constructions dessus.