Hi again,

I'm trying to track an issue making use of Marshmallow-SQLAlchemy (I've 
also seen what looks like a very similar problem in ColanderAlchemy, but I 
haven't dug into that yet). I think it might have something to do with the 
mapper_configured event and whether the class is truly fully configured at 
that point. Possibly I'm misunderstanding what that means.

If I do something like this:

import sqlalchemy as sa
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker, relationship
from sqlalchemy import event
from sqlalchemy.orm import mapper

engine = sa.create_engine('sqlite:///:memory:')
session = scoped_session(sessionmaker(bind=engine))
Base = declarative_base()

class Author(Base):
    __tablename__ = 'authors'
    id = sa.Column(sa.Integer, primary_key=True)
    name = sa.Column(sa.String)

    def __repr__(self):
        return '<Author(name={self.name!r})>'.format(self=self)

class Book(Base):
    __tablename__ = 'books'
    id = sa.Column(sa.Integer, primary_key=True)
    title = sa.Column(sa.String)
    author_id = sa.Column(sa.Integer, sa.ForeignKey('authors.id'))
    author = relationship("Author", backref='books')

    def __repr__(self):
        return '<Book(title={self.title!r})>'.format(self=self)

Base.metadata.create_all(engine)

def print_stuff(mapper, class_):
    print getattr(class_, 'books')

event.listen(mapper, 'mapper_configured', print_stuff)

author = Author(name='Chuck Paluhniuk')

When I run this code, the result is that when Author is first used, the 
'mapper_configured' event will fire and my print_stuff function callback 
will run.

The results is this:

Traceback (most recent call last):
  File "marshmallow_experiment.py", line 37, in <module>
    author = Author(name='Chuck Paluhniuk')
  File "<string>", line 2, in __init__
  File 
"/usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/orm/instrumentation.py",
 
line 347, in _new_state_if_none
    state = self._state_constructor(instance, self)
  File 
"/usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py",
 
line 747, in __get__
    obj.__dict__[self.__name__] = result = self.fget(obj)
  File 
"/usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/orm/instrumentation.py",
 
line 177, in _state_constructor
    self.dispatch.first_init(self, self.class_)
  File 
"/usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/event/attr.py",
 
line 258, in __call__
    fn(*args, **kw)
  File 
"/usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py",
 
line 2792, in _event_on_first_init
    configure_mappers()
  File 
"/usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py",
 
line 2691, in configure_mappers
    mapper, mapper.class_)
  File 
"/usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/event/attr.py",
 
line 218, in __call__
    fn(*args, **kw)
  File 
"/usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/orm/events.py",
 
line 527, in wrap
    fn(*arg, **kw)
  File "marshmallow_experiment.py", line 33, in print_stuff
    print getattr(class_, 'books')
AttributeError: type object 'Author' has no attribute 'books'

Should not the backref be configured at that point? What I'm trying to do 
is use the mapper_configured event in order to autocreate 
marshamallow-sqlalchemy schemas, which works fine except that because books 
doesn't exist at the point when the callback function runs, the resulting 
schema is also missing the books part.

If I do this instead:

author = Author(name='Chuck Paluhniuk')
print_stuff(None, Author)

Then the result is:

Author.books

I.e. It is now there.

Thanks again for your continued excellent support.

Douglas

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to