db-torque-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dave Newton <d...@solaraccess.com>
Subject "Dynamic" foreign keys?
Date Thu, 26 Jun 2003 22:07:08 GMT
Hola all,

I'm wondering if I can/how I would do the following under Torque.

I have several tables that can have zero or more address records (each
of which can have zero or more phone records, but whatever). 

I'd rather not have a separate mapping table for each table that can
have address info, so I've been using an "external" table name and id
inside the address record (so an address might be for the user table, id
50; or the job table, id 75; etc.)

I want to make this relatively automagic under Torque so I can set up
the foreign keys appropriately (i.e., I create a user, I create an
address, I do a user.addAddress(newAddress), a user.save(), and the
world is a happy place (and the reverse should also happen, naturally.)

I've tried poking the addColumn and addForeignKey methods of TableMap
with pointy sticks but the best I can do is make an exception saying
that the "exname" column in the address table can't be null (which is
quite true, of course :)

I'm not sure if I'm just using the TableMap functions inappropriately
(i.e., you can't add anything dynamic like that; I don't want the user's
fake exname field saved in the database, I want to pretend it's there
and perma-set it to the class's appropriate Peer-class TABLE_NAME value)
or if I'm using them incorrectly (you _can_ do this, I'm just doing it
poorly) or... something else.

I also tried putting getExname/setExname calls in the User bean to
further fake it out, but no dice.

I've only played with the TableMap routines under Jython (after adding a
public method to get it) but hopefully the trashy jython code below will
give you an idea of what I'm trying to do.

In a nutshell I want to link n tables to 1 table based on a tablename/id
pair and not have to write code to do it :D

I can email/webify the schema files if necessary.

Thanks,
Dave

--- crappy jython play code ---

from org.apache.torque import Torque
Torque.init("Torque.properties")

from com.solaraccess.tbean import UserPeer as UP
from java.lang import Integer, String

# I've also tried things like user.EXNAME etc.
map = UP.getTablemap()
map.addColumn("exname", String(""))
map.addForeignKey("exname", String(""), "contact", "exname")

from java.util import Date
from com.solaraccess.tbean import User, Contact

u = User(fname="Dave", lname="Newton", email="dave@sa.com",
password="goaway", startdate=Date())
c = Contact(addr1="3 Elm St")
u.addContact(c)
u.save()



Mime
View raw message