Re: [Modeling-users] Foreign Keys
Status: Abandoned
Brought to you by:
sbigaret
|
From: Yannick G. <yan...@sa...> - 2003-07-23 15:45:37
|
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On July 23, 2003 10:09 am, Sebastien Bigaret wrote:
> > But if I have some of the records that have been
> > deleted, the PKs are not in a continuous sequence so FKs become out of
> > synch.
>
> Ok. But could you tell how you are doing this exactly? Are you
> building/inserting-in-ec new objects, then saving changes, or are you
> using some of the low-level functionalities to execute raw sql inserts,
> or something else? Asking this, because I'm trying to understand at
> which point exactly you need to provide the PK, where it is stored, etc.
Ehh humm like this :
exec ("global %s" % childClassName)
exec (importCmd % (childClassName, childClassName))
posElem = xpath.Evaluate(("./%s/text()" % fkStr), elem)[0]
curPos = int(posElem.nodeValue)
i18nPath = '/db_dump/%s[%s/text()="%d"]' % ( childClassName,
fkStr,
curPos )
i18nElems = xpath.Evaluate(i18nPath, self.i18nDoc)
# create i18ns
for i18nElem in i18nElems:
i18nObj = eval("%s()" % childClassName)
self.fillObject(i18nObj, i18nElem)
obj.addObjectToBothSidesOfRelationshipWithKey(i18nObj, 'i18n')
self.ec.insertObject(i18nObj)
self.ec.insertObject(obj)
I process the table dumps 2 by 2, one for the master, one for i18ns.
I must admit that I don't know how I will restore the more complex
relations. The way I do it now fix the missing PKs problem because I
find the matching ones in the i18n dump with i18nPath but it's a hack,
a really slow hack. I have to do a XPath query on a 1400 nodes file
700 time : 300 secs avg.
So skiping PKs would be much more simple but having the framework
doing the dump and restoration would be even beter. I do the dump
like this (phpMyAdmin is full Latin-1) :
if __name__ == "__main__":
if not len(sys.argv) == 2:
print "USAGE : dump_xml.py <entity-name>"
sys.exit(1)
sys.path.append("..")
entityName = sys.argv[1]
ec = EditingContext()
mm = ModelManager(ec)
importCmd = "from Autogen.%s import %s" % (entityName, entityName)
exec(importCmd)
objs = mm.fetch(entityName)
# get the primary key name
key = ec.globalIDForObject(objs[0]).keyValues().keys()[0]
objs = map(lambda obj:mm.snapshot(obj), objs)
objs = sortById(objs, key)
domDoc = getDomDoc()
for obj in objs:
elem = domDoc.createElement(entityName)
for key in obj.keys():
tagName = externalNameForInternalName(key).lower()
property = domDoc.createElement(tagName)
if key != "i18n":
elem.appendChild(property)
dataStr = toStr(obj[key])
data = domDoc.createTextNode(dataStr)
property.appendChild(data)
xpath.Evaluate("/db_dump", domDoc)[0].appendChild(elem)
PrettyPrint(domDoc)
Yes, i18n is hardcoded... : \
- --
Yannick Gingras
Byte Gardener, Savoir-faire Linux inc.
(514) 276-5468
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
iD8DBQE/Hq2brhy5Fqn/MRARAgH1AJwLLuIHR5WxlGkRv8vjXuvveMH2TACeLpZi
EuosOQLR5OrqkhwSJU13TTw=
=+ReG
-----END PGP SIGNATURE-----
|