Return-Path: Mailing-List: contact torque-user-help@db.apache.org; run by ezmlm Delivered-To: mailing list torque-user@db.apache.org Received: (qmail 45157 invoked from network); 26 Jun 2003 22:11:16 -0000 Received: from s5.servlets.net (209.221.135.8) by daedalus.apache.org with SMTP; 26 Jun 2003 22:11:16 -0000 Received: from localhost.localdomain ([208.50.239.210]) by s5.servlets.net (8.9.3/8.9.3) with ESMTP id PAA19922 for ; Thu, 26 Jun 2003 15:11:21 -0700 Subject: "Dynamic" foreign keys? From: Dave Newton To: torque-user@db.apache.org Content-Type: text/plain Content-Transfer-Encoding: 7bit X-Mailer: Ximian Evolution 1.0.8-3mdk Date: 26 Jun 2003 18:07:08 -0400 Message-Id: <1056665233.10893.924.camel@localhost.localdomain> Mime-Version: 1.0 X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N 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()