From nigel at solarsoftware.com.au Tue Jun 22 14:09:40 2004 From: nigel at solarsoftware.com.au (Nigel Sim) Date: Tue Jun 22 11:04:01 2004 Subject: [orm-devel] Re: [orm-users] one2one using the primary key as the foriegn key In-Reply-To: <40D83563.3010002@t4w.de> References: <1087903752.3557.8.camel@bakunin> <40D83563.3010002@t4w.de> Message-ID: <1087906180.3557.19.camel@bakunin> Thanks for the prompt reply. I'll prepend this by saying I really like your work, and I have been using it successfully for a while, but I need to make it work in a situation which is slightly different to how it was designed, and really want to know if there is a quick fix before I build a new one2one relationship class. In this situation the tables are already in use and can't be changed. On Tue, 2004-06-22 at 15:34 +0200, Diedrich Vorberg wrote: > Hi Nigel, > > > How do you go about using a one2one mapping in the following situation. > > |--------| |-----------| > > | Person | | Picture | > > ---------- ------------- > > | ID |<->| person_ID | > > | Name | | etc | > > | etc | | | > > |--------| |-----------| > this is not the setup needed by a one2one relationship. It should look > like this: > > |--------| |-----------| > | Person | | Picture | > ---------- ------------- > | ID | | | > | ImageId|<->| | > | Name | | etc | > | etc | | | > |--------| |-----------| > I agree that method works, but really Person.imageId is redundant, there will only ever be one picture for each person. Having a separate ID for each one2one table will just get messy, and I am linking into an existing system so changing this isn't an option. CREATE TABLE `person` ( `id` int(11) NOT NULL auto_increment, `name` varchar(255) NOT NULL default '', `number` varchar(50) NOT NULL default '', PRIMARY KEY (`id`) ) CREATE TABLE `picture` ( `person_id` int(11) NOT NULL default '0', `date` datetime NOT NULL default '0000-00-00 00:00:00', `filename` varchar(255) NOT NULL default '', `caption` varchar(255) NOT NULL default '', PRIMARY KEY (`person_id`) ) ------------------------------------------------ from connect import ds from orm.dbclass import dbclass from orm.relationships import * from orm.errors import * from orm.columns import * from orm.adapters.mysql.columns import * # The datamodel: class Person(dbclass): tableName = "person" columns = { "id": autoincrement(), "name": varchar(), "number": varchar() } class Picture(dbclass): tableName = "picture" primaryKey = "person" columns = { "person": one2one(Person), "date": date(), "caption": varchar(), "filename": varchar()} Person.columns["picture"] = one2one(Picture) def main(): new_person = Person(name="nigel",number="555 1234") ds().insert(new_person) new_picture = Picture(caption="nigel") new_picture.person = new_person <<< Causes the following error main() ------------------------------------------------ INSERT INTO person (number, name) VALUES ('555 1234', 'nigel') SELECT LAST_INSERT_ID() SELECT LAST_INSERT_ID() Traceback (most recent call last): File "data.py", line 35, in ? main() File "data.py", line 32, in main new_picture.person = new_person File "/usr/lib/python2.3/site-packages/orm/dbclass.py", line 354, in __setattr__ self._data[name].set(value) File "/usr/lib/python2.3/site-packages/orm/relationships.py", line 322, in set self.ds().execute("UPDATE %s SET %s=NULL WHERE %s=%s" % \ AttributeError: 'NoneType' object has no attribute 'execute' From dv at t4w.de Tue Jun 22 15:34:27 2004 From: dv at t4w.de (Diedrich Vorberg) Date: Tue Jun 22 11:42:29 2004 Subject: [orm-devel] Re: [orm-users] one2one using the primary key as the foriegn key In-Reply-To: <1087903752.3557.8.camel@bakunin> References: <1087903752.3557.8.camel@bakunin> Message-ID: <40D83563.3010002@t4w.de> Hi Nigel, > How do you go about using a one2one mapping in the following situation. > |--------| |-----------| > | Person | | Picture | > ---------- ------------- > | ID |<->| person_ID | > | Name | | etc | > | etc | | | > |--------| |-----------| this is not the setup needed by a one2one relationship. It should look like this: |--------| |-----------| | Person | | Picture | ---------- ------------- | ID | | | | ImageId|<->| | | Name | | etc | | etc | | | |--------| |-----------| > Ie, instead of having a separate foreign key in Person, I want its > primary key to be used. If I try to do this at the moment I can create > and save person, then I create Picture and as soon as I try to set > person_id=person it tries to run an UPDATE on the Picture table and that > crashes the process. Setting person.image = image should not alter the image table at all, something is going wrong here. > Alternatively I can insert a Picture, save it > setting picture.person_id=0, and then set person_id=person. But you > shouldn't have to insert picture until it is in its final state. Having > person_id=0 is having the database in an inconsistent state, which is > bad. Well, could you post your person and picture classes and a few lines of Python code that makes the program go wrong? Also the person and picutre SQL definitions would be nice. If you don't want to post them on the list you may post them to me privately. Something basic is screwed up here, I guess... Diedrich -- Diedrich Vorberg .---. / \ http://tux4web.de Tel: 02302 425269 / \ ((__-^^-,-^^-__)) \.O-O./ `-_---' `---_-' "Unix is simple, but it takes a genius /`\_/`\ `--|o` 'o|--' to understand the simplicity." // _ \\ \ ` / - Dennis Ritchie | \ )|_ ): :( /`\_`> <_/ \ :o_o: Associate Member of the FSF #1245 \__/'---'\__/ "-"