Re: [sqlalchemy] foreign key to *any* other class?

2010-01-11 Thread Jules Stevenson
Thanks Michael, very useful.
-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.




[sqlalchemy] foreign key to *any* other class?

2010-01-10 Thread Jules Stevenson
Sorry for the bad subject, and probably incorrect terminology but is
it possible to set up a table that would allow a foreign key link (or
any other technique) that would enable it to bind against *any* other
mapped class?

The context for this is there is a DB table that stores tasks to be
executed. These tasks could relate to lots and lots of other mapped
classes, and essentially I'd just like to be able to call:

sentinelTask.dbObject

where dbObject could be a user, project, client or any other allready
mapped class.

Is this possible? What should the DB structure be to set this sort of
relationship up.

Sorry if this is just plain stupid, any pointers much appreciated.

Jules
-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.




Re: [sqlalchemy] foreign key to *any* other class?

2010-01-10 Thread Michael Bayer

On Jan 10, 2010, at 2:55 PM, Jules Stevenson wrote:

 Sorry for the bad subject, and probably incorrect terminology but is
 it possible to set up a table that would allow a foreign key link (or
 any other technique) that would enable it to bind against *any* other
 mapped class?
 
 The context for this is there is a DB table that stores tasks to be
 executed. These tasks could relate to lots and lots of other mapped
 classes, and essentially I'd just like to be able to call:
 
 sentinelTask.dbObject
 
 where dbObject could be a user, project, client or any other allready
 mapped class.
 
 Is this possible? What should the DB structure be to set this sort of
 relationship up.
 
 Sorry if this is just plain stupid, any pointers much appreciated.

In order from easiest to most tedious:

method 1:

- put individual foreign key columns on task relating to each kind of target 
object. On your class, dbObject just checks each of the related 
attributes for non-None, then returns that.

method 2:

- user, project client etc. all have a foreign key back to your task table, 
not the other way around.   As before dbObject checks among several related 
attributes.

method 3:

- use an association table for each association of task to X.   The basic 
idea of this is described at http://techspot.zzzeek.org/?p=13 .   The examples 
there use an older style of SQLA querying but are easy to update.

method 4:

- user, project, client etc. are all inherited from a base table, and your 
task table contains a foreign key to that.   See the docs on joined table 
inheritance for that.   If you're already using joined then this would be the 
easiest method.







 
 Jules
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To post to this group, send email to sqlalch...@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.
 
 

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.