Am 25.01.2011 um 14:36 schrieb luc:

Hi,

consider the following model. After the following pattern of code:

memento(m1) #####
memento(m2)
....
session.commit()

I get an exception whose traceback runs through the places I marked
with #####

sqlalchemy.exc.IntegrityError: (IntegrityError)
xxx_chemical_element.symbol may not be NULL u'INSERT INTO
xxx_chemical_element DEFAULT VALUES' ()

but I fail to see where a chemical_element could be created without
its attribute symbol getting a non-trivial value. And no, the argument
symbol below is a non-trivial string.

class chemical_element(Entity):

 symbol = Field(String, primary_key=True)
 mementoes = ManyToMany('memento')

 def __init__(self, symbol):
   if not self.get_by(symbol=symbol) is None: #####
     self.symbol = symbol

class space_group(Entity):

 number = Field(Integer)
 hall = Field(String, primary_key=True)
 mementoes = OneToMany('memento')

 def __init__(self, space_group_info):
   self.number = space_group_info.type().number()
   self.hall   = space_group_info.type().hall_symbol()

class memento(Entity):

 name = Field(String, primary_key=True)
 pickle = Field(PickleType)
 space_group = ManyToOne('space_group')
 chemical_elements = ManyToMany('chemical_element')

 def __init__(self, m):
   self.name = m.name
   self.pickle = m
   self.space_group = space_group(m.expected_sgi)
   for e in m.asymmetric_unit_formula.iterkeys():
     self.chemical_elements.append(chemical_element(symbol=e)) #####

I had problems when being to crazy with constructors. The least thing you are missing here is that you do not call super.__init__. And you possibly need a session.flush() to persist the actual entity before putting more stuff there.

In general, it's good advice to not do more than providing default values in constructors.

Diez

--
You received this message because you are subscribed to the Google Groups 
"SQLElixir" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlelixir?hl=en.

Reply via email to