On Jan 31, 2012, at 7:08 AM, Torsten Landschoff wrote:

> On Tue, 2012-01-31 at 12:52 +0100, Torsten Landschoff wrote:
> 
>> However, with SQLAlchemy 0.7.5 I get
>> 
>> $ python new_instance.py 
>> Traceback (most recent call last):
>>  File "new_instance.py", line 27, in <module>
>>    session.commit()
>>  [...]
>>  File 
>> "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.7.5dev-py2.6-linux-x86_64.egg/sqlalchemy/engine/default.py",
>>  line 330, in do_execute
>>    cursor.execute(statement, parameters)
>> sqlalchemy.exc.IntegrityError: (IntegrityError) people.type may not be NULL 
>> u'INSERT INTO people (type) VALUES (?)' (None,)


If you're using new_instance() to create the instance, then you are 
deserializing data from somewhere, why isn't the discriminator value, which is 
after all one of the column values in the table, not present in this 
deserialization ?

Right now the event in question is emitted only from the __init__() method of 
your object, which is obviously what you don't call when deserializing.    The 
discriminator value is now considered to be just another instance variable that 
you can change freely - a default for it is configured from __init__().

Anyway the event here is the "init" event which you can emit from classmanager:

    manager.dispatch.init(state, args, kw)

where "args", "kw" are what would normally be sent to __init__, but can just be 
blank here.

Invoking the events is not entirely public API, though.  





> 
> I found a workaround:
> 
> --- a/new_instance.py 2012-01-31 12:50:44.510525675 +0100
> +++ b/new_instance.py 2012-01-31 13:05:52.110514861 +0100
> @@ -1,6 +1,6 @@
> from sqlalchemy import *
> from sqlalchemy.orm import *
> -from sqlalchemy.orm.attributes import manager_of_class
> +from sqlalchemy.orm.attributes import manager_of_class, instance_state
> from sqlalchemy.ext.declarative import declarative_base
> 
> 
> @@ -22,7 +22,11 @@
> Base.metadata.create_all(engine)
> session = sessionmaker(engine)()
> 
> -e = manager_of_class(Engineer).new_instance()
> +manager = manager_of_class(Engineer)
> +e = manager.new_instance()
> +state = instance_state(e)
> +manager.mapper._set_polymorphic_identity(state)
> +
> session.add(e)
> session.commit()
> 
> But this is leaving the area of documented API. I found out that
> SQLAlchemy actually initializes the polymorphic_identity column by
> registering an on_init event. I'd rather trigger that but I did not yet
> find out how to do that.
> 
> Thanks, Torsten
> 
> -- 
> DYNAmore Gesellschaft fuer Ingenieurdienstleistungen mbH
> Torsten Landschoff
> 
> Office Dresden
> Tel: +49-(0)351-4519587
> Fax: +49-(0)351-4519561
> 
> mailto:torsten.landsch...@dynamore.de
> http://www.dynamore.de
> 
> Registration court: Stuttgart, HRB 733694
> Managing director: Prof. Dr. Karl Schweizerhof, Dipl.-Math. Ulrich Franz
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> To unsubscribe from this group, send email to 
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to