Re: [Modeling-users] Foreign Keys
Status: Abandoned
Brought to you by:
sbigaret
From: Sebastien B. <sbi...@us...> - 2003-07-21 14:59:02
|
Hi, Yannick Gingras <yan...@sa...> wrote: > Hi, I'd like to set my foreign keys by hand ex.: >=20 > i18n =3D I18N() > ec.insert(i18n) > i18n.setMasterId(masterSnapshot["id"]) > ec.saveChanges() >=20 > Is there a way to do it without breaking anything ? >=20 > Since I switched to raw fetches, I end-up with a dict that is unusable wi= th=20 > addObjectToBothSidesOfRelationship(). In fact, having=20 > addObjectToBothSidesOfRelationship() accept snapshots would be nice. First, a general answer to this. As said at http://modeling.sf.net/UserGuide/ec-fetch-raw-rows.html, when you use raw row fetching you forego most of the capabilities of the framework. This includes tracking changes (no objects are even created, so the framework cannot keep track of the changes), all the more so manipulating relationships and saving the changes. I should probably make it even clearer in the documentation. So what? Fortunately you're not stuck. In general, you have two solutions at hand: =20=20 - either fall back to raw sql. Probably not what you want... =20=20 - or revert the row you want to change into a real object, using EC.faultForRawRow(), then make your changes and save them. Now back to your specific problem: based on your code, I assume you have the following model: master <--->> i18n (-> is to-one, ->> is to-many) So you'll do something like this, assuming that the master entity's name is 'Master' and the relationship' name i18n--->>master is: 'master' and the inverse is 'i18ns': >>> i18n =3D I18N() >>> ec.insert(i18n) >>> master=3Dec.faultForRawRow(masterSnapshot, 'Master') >>> i18n.addObjectToBothSidesOfRelationshipWithKey(master, 'master') >>> ec.saveChanges() or its alternate equivalent: >>> i18n =3D I18N() >>> ec.insert(i18n) >>> master=3Dec.faultForRawRow(masterSnapshot) >>> master.addToI18ns(i18n) >>> i18n.setMaster(master) >>> ec.saveChanges() Naturally, this implies that the 'master' object is fetched and initialized within the EditingContext. Does it fulfill your needs? -- S=E9bastien. |