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,)

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.

Reply via email to