Hello again,

for a while I want to upgrade our application to use SQLAlchemy 0.7.x.
However I am running into changed behaviour wrt.
ClassManager.new_instance. The behaviour I rely on was discussed here:

http://article.gmane.org/gmane.comp.python.sqlalchemy.user/28746

Basically, I want to create an empty instance of a mapped class and
unserialize my data into it. I am not using Pickle because of security
and compatibility fears.

The attached sample program worked fine with SQLAlchemy 0.6.8:

$ python new_instance.py 
<__main__.Engineer object at 0x10774d0>

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'd really like to use 0.7.5 but I don't know how to replace
new_instance. I attached the sample program and a suggested patch for
the documentation: In the declarative example, the type column is
nullable.

I found out the hard way that it should rather not be. Having a
non-nullable type column at least unconvers the problem when writing to
the database, not when reading the corrupted database :-)

Greetings, Torsten

-- 
DYNAmore Gesellschaft fuer Ingenieurdienstleistungen mbH
Torsten Landschoff

Office Dresden
Tel: +49-(0)351-4519587
Fax: +49-(0)351-4519561

mailto:[email protected]
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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.orm.attributes import manager_of_class
from sqlalchemy.ext.declarative import declarative_base


Base = declarative_base()

class Person(Base):
    __tablename__ = 'people'
    id = Column(Integer, primary_key=True)
    discriminator = Column('type', String(50), nullable=False)
    __mapper_args__ = {'polymorphic_on': discriminator}

class Engineer(Person):
    __tablename__ = 'engineers'
    __mapper_args__ = {'polymorphic_identity': 'engineer'}
    id = Column(Integer, ForeignKey('people.id'), primary_key=True)
    primary_language = Column(String(50))

engine = create_engine("sqlite:///")
Base.metadata.create_all(engine)
session = sessionmaker(engine)()

e = manager_of_class(Engineer).new_instance()
session.add(e)
session.commit()

print session.query(Engineer).first()
diff -r ebe9514a69a4 lib/sqlalchemy/ext/declarative.py
--- a/lib/sqlalchemy/ext/declarative.py	Sat Jan 28 17:43:13 2012 -0500
+++ b/lib/sqlalchemy/ext/declarative.py	Tue Jan 31 12:50:35 2012 +0100
@@ -389,7 +389,7 @@
     class Person(Base):
         __tablename__ = 'people'
         id = Column(Integer, primary_key=True)
-        discriminator = Column('type', String(50))
+        discriminator = Column('type', String(50), nullable=False)
         __mapper_args__ = {'polymorphic_on': discriminator}
 
     class Engineer(Person):

Reply via email to