If, after generation of code, you need to move the generated package to some other package, or if you need to move a class to some other module, just adjust you model to reflect your change. The following adjustments should be made:
'packageName'
'moduleName'
and 'className'
The only limitation is that all classes corresponding to entities in a model
should be located within the same package (the one identified by the field
'packageName'
in model's properties).
0
(integer zero)
This method is the best way to get the values that will be stored for an object at a given state in the database when changes are saved. It returns a dictionary from which the value of the primary key can be retrieved. Be sure to carefully read its documentation before using it.
This method does not require that the primary key is made a class property.
>>> object=ec.fetch('Writer', qualifier='age<50')[0] >>> object.globalID().keyValues() {'id': 1}
So if you need to access the PK for objects of a given class, you can add a method like this one:
def pk(self): "Returns the pk's value" gid=self.globalID() if not gid: return None if gid.isTemporary(): # temporary gid: object has been inserted but not saved yet. # Change this to return any value you find more appropriate return gid return gid.keyValues()['id']
Note: CustomObject.globalID() can return None
when an object
has just been inserted into an EditingContext but has not been saved
yet.
As a general rule, foreign keys should not be made class properties; this is an even stricter rule than the one for primary keys, because the framework does not update foreign keys values in objects that define them as class properties (but of course, they will saved as expected in the database).
The reason is that a foreign key is usually used to store the piece of
information needed to store in the database a to-one relationship at the
object level. Say you have an entity, Writer
related with the entity
Book
in a one-to-many relationship; the table for entity Book
stores the primary key value for the corresponding Writer
in a foreign
key FK_Writer_id
. Suppose now that a book is moved from one author to
another: at the object-level, the book is removed to one author's set of books
and added to the others, while the book itself gets a new author:
>>> writer1.removeFromBooks(book) >>> book.setAuthor(writer2) >>> writer2.addToBooks(book)
If the foreign key Book.FK_Writer_id
is a class property, it is now out
of sync with the book's author, because it stores the former author's id while
it has been assigned to an other one.
However, there are several alternate solutions for accessing a foreign key value, if you really insist on doing this:
CustomObject.snapshot_raw()
for
details.
You can also do it manually, by traversing the relationship to get its primary
key, for example by adding such a method to your Book
:
def getFKWriterId(self): if self.getWriter() is None: return None else: w_gid=self.getWriter().globalID() if w_gid.isTemporary(): # temporary gid: object has been inserted but not saved yet. # Change this to return any value you find more appropriate return w_gid else: return w_gid.keyValues()
Comments are welcome: Sebastien Bigaret / Modeling Home Page