Original __init__ from the mapped class is not called when fetching from the DB

Issue #391 resolved
Former user created an issue

Hello, I posted a message to the ML some hourse ago regarding this problem; shortly after, I realized it was huger than I thought; simply, SA doesn't call the original init of a mapped class if the object is fetched from the DB.

Also, since the new, mapper-aware init is a closure, and the original init is passed to it, there seems to be no obvious way to call the original init 'manually' (e.g. in a MapperExtension populate_instance method).

I haven't found any mention of this in the docs, so I suppose it's not a wanted behaviour...

from sqlalchemy import *
import sys

connect_params = "postgres://user:12345@127.0.0.1:5432/kiwi"

engine = create_engine(connect_params, echo=False, convert_unicode=True)

metadata = BoundMetaData(engine)


prove = Table("prove", metadata,
          Column("id", Integer, Sequence("prove_id_seq"), primary_key=True, nullable=False),
          Column("testo", String(30)),
          Column("numero_lungo", Integer),
          )

metadata.create_all()

class Prova(object):

    def __init__(self, testattr="default"):
        print "initialization done!"
        self.testattr = testattr
        super(Prova, self).__init__()

m = mapper(Prova, prove)

a = Prova()
a.id = 300
a.testo = "abc"
print a.testattr

session = create_session()
session.save(a)
session.flush()
session.clear()

query = session.query(Prova)

b = query.select(prove.c.id == 300)[0](0)

print b.testattr

output:

initialization done!
default
Traceback (most recent call last):
  File "/home/alan/pythons/TestVari/sa_init.py", line 42, in <module>
    print b.testattr
AttributeError: 'Prova' object has no attribute 'testattr'

Comments (2)

  1. Log in to comment