Hello everyone,

I need to do smth like this in a mapper() call for table 'hosts':

'Earliest_reservation':relation(Reservation, order_by=Reservation.id, 
backref='hosts'),
'Reservations':relation(Reservation, 
secondary=reservation_hosts_assoc_table, backref='hosts')

That is, I need to create "links" between tables 'hosts' and 
'reservation': one is normal ForeignKey of reservation.id in hosts 
table, and another is via association table (reservation_hosts_assoc_table).

The reason is somewhat contrived, but I think reasonable: efficiency. I 
need to display the date of earliest reservation in the HTML table of 
hosts. If I have 400 hosts, then without earliest reservation id in the 
hosts table this means 400 separate queries to reservation table. 
Obviously, I need to avoid that. Somehow.



hosts_table = Table('hosts',md,
Column('id',Integer,primary_key=True),
Column('IP',String),
Column('HostName',String),
Column('Location',String),
Column('Architecture_id',Integer,ForeignKey('architecture.id')),
Column('OS_Kind_id',Integer,ForeignKey('os_kind.id')),
Column('OS_version_id',Integer,ForeignKey('os_version.id')),
Column('Additional_info',String),
#Column('End_Date',SLDate),
Column('Column_12',String),
Column('Column_13',String),
Column('Email_id',Integer,ForeignKey('email.id')),
Column('Username',String),
Column('Password',String),
Column('Alias',String),
Column('Virtualization_id',Integer,ForeignKey('virtualization.id')),
Column('Shareable',SLBoolean),
Column('Shareable_between_projects',String),
Column('Notes',String),
Column('CPU',String),
Column('RAM',String),
Column('Column_24',String),
Column('Batch',String),
Column('ASSET',String),
Column('Owner',String),
Column('SSH_KEY_PRESENT',String),
Column('Machine_Type_Model',String),
Column('MAC_ADDRESS_ETH_0',String),
Column('Physical_Box',SLBoolean),
Column('Up_n_running',SLBoolean),
Column('Available',SLBoolean),
Column('Earliest_reservation_id',Integer,ForeignKey('reservation.id')),
Column('Project_id',Integer,ForeignKey('project.id')))

reservation_table = Table('reservation', md,
Column('id',Integer,primary_key=True),
Column('Start_Date',SLDate),
Column('End_Date',SLDate),
Column('Status', String),
Column('Businessneed', String),
Column('Notetohwrep',String),
Column('Email_id',Integer,ForeignKey('email.id')),
Column('Project_id',Integer,ForeignKey('project.id'))
)

reservation_hosts_assoc_table = Table('reservation_hosts', md,
Column('Reservation_id',Integer,ForeignKey('reservation.id')),
Column('Host_id',Integer,ForeignKey('hosts.id'))
)


mapper(Reservation, reservation_table, 
properties={'Email':relation(Email,order_by=Email.id,backref='reservation'),
'Project':relation(Project, order_by=Project.id, backref='reservation'),
'Hosts':relation(Host, secondary=reservation_hosts_assoc_table, 
backref='reservation')})

mapper(Host, hosts_table,
properties={'Architecture':relation(Architecture, 
order_by=Architecture.id, backref='hosts'),
'OS_Kind':relation(OS_Kind, order_by=OS_Kind.id, backref='hosts'),
'OS_version':relation(OS_version, order_by=OS_version.id, 
backref='hosts'), 'Email':relation(Email, order_by=Email.id, 
backref='hosts'), 'Virtualization':relation(Virtualization, 
order_by=Virtualization.id, backref='hosts'),
'Project':relation(Project, order_by=Project.id, backref='hosts'),
'Earliest_reservation':relation(Reservation, order_by=Reservation.id, 
backref='hosts'),
'Reservations':relation(Reservation, 
secondary=reservation_hosts_assoc_table, backref='hosts')
})


--~--~---------~--~----~------------~-------~--~----~
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 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to