Hi, Michael,
here is the stack trace:
File "/home/andrey/projects/art/artFlask/api/artList.py", line 30, in post
item = app_ctx.create_item_from_context()
File "/home/andrey/projects/art/artFlask/utils/app_ctx.py", line 53, in
create_item_from_context
item = ModelClass(**data)
File "<string>", line 2, in __init__
File
"/home/andrey/envs/art_flask2/lib/python2.7/site-packages/sqlalchemy/orm/instrumentation.py",
line 322, in _new_state_if_none
state = self._state_constructor(instance, self)
File
"/home/andrey/envs/art_flask2/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py",
line 712, in __get__
obj.__dict__[self.__name__] = result = self.fget(obj)
File
"/home/andrey/envs/art_flask2/lib/python2.7/site-packages/sqlalchemy/orm/instrumentation.py",
line 155, in _state_constructor
self.dispatch.first_init(self, self.class_)
File
"/home/andrey/envs/art_flask2/lib/python2.7/site-packages/sqlalchemy/event/attr.py",
line 257, in __call__
fn(*args, **kw)
File
"/home/andrey/envs/art_flask2/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py",
line 2673, in _event_on_first_init
configure_mappers()
File
"/home/andrey/envs/art_flask2/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py",
line 2569, in configure_mappers
mapper._post_configure_properties()
File
"/home/andrey/envs/art_flask2/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py",
line 1682, in _post_configure_properties
prop.init()
File
"/home/andrey/envs/art_flask2/lib/python2.7/site-packages/sqlalchemy/orm/interfaces.py",
line 143, in init
self.do_init()
File
"/home/andrey/envs/art_flask2/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py",
line 1510, in do_init
self._process_dependent_arguments()
File
"/home/andrey/envs/art_flask2/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py",
line 1567, in _process_dependent_arguments
self.target = self.mapper.mapped_table
File
"/home/andrey/envs/art_flask2/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py",
line 712, in __get__
obj.__dict__[self.__name__] = result = self.fget(obj)
File
"/home/andrey/envs/art_flask2/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py",
line 1484, in mapper
argument = self.argument()
File
"/home/andrey/envs/art_flask2/lib/python2.7/site-packages/sqlalchemy/ext/declarative/clsregistry.py",
line 275, in __call__
(self.prop.parent, self.arg, n.args[0], self.cls)
InvalidRequestError: When initializing mapper Mapper|Artwork|artwork,
expression 'Person' failed to locate a name ("name 'Person' is not
defined"). If this is a class name, consider adding this relationship() to
the <class 'model.Artwork'> class after both dependent classes have been
defined.
Does it tell you anything ?
среда, 2 июля 2014 г., 17:38:55 UTC+3 пользователь Michael Bayer написал:
>
>
> On 7/2/14, 10:21 AM, [email protected] <javascript:> wrote:
>
> Hi Michael, thank you for the answer.
> Both classes are in the same file so I don't see how it could be possible
> that one class is used while other is not imported.
> Could you help me with that ?
>
>
> that would mean you're doing something that is invoking Artwork as a
> mapped class and causing the mapper config step to occur before it gets
> down to Person. The stack trace here would show exactly where that
> originates.
>
>
>
>
>
>
> Andrey
>
> вторник, 1 июля 2014 г., 21:05:11 UTC+3 пользователь Michael Bayer
> написал:
>>
>>
>> On 7/1/14, 1:17 PM, [email protected] wrote:
>>
>> I have two classes, Artwork and Person. Artwork has a relationship to
>> Person.
>>
>> However, when I try to use them, I get an error thrown:
>>
>> InvalidRequestError: When initializing mapper Mapper|Artwork|artwork,
>>> expression 'Person' failed to locate a name ("name 'Person' is not
>>> defined"). If this is a class name, consider adding this relationship() to
>>> the <class 'model.Artwork'> class after both dependent classes have been
>>> defined.
>>
>>
>>
>> Here are the classes themselves, defined in model/__init__.py
>>
>> class Artwork(db.Model, SimpleSerializeMixin):
>> id = db.Column(db.Integer, primary_key=True)
>> ....
>> artist_id = db.Column(db.String(256), db.ForeignKey('person.sub'))
>> artist = db.relationship('Person', backref='artworks')
>>
>> class Person(db.Model, SimpleSerializeMixin):
>> sub = db.Column(db.String(256), primary_key=True)
>>
>>
>>
>> I checked with debugger and in sqlalchemy/ext/declarative/clsregistry.py
>> (_class_resolver.__call__())
>> there is a line:
>> x = eval(self.arg, globals(), self._dict)
>> No "Person" or "Artwork" or any other class defined in the file are
>> present in globals(). self._dict is empty
>> So it fails with an NameError exception.
>>
>> What could be the issue ?
>>
>>
>> it's usually that the Person code wasn't run, e.g. that the module in
>> which it is located was not imported, before you tried to use the Artwork
>> class. All the tables/classes can be introduced to the Python interpreter
>> in any order, but once you try to "use" the mapping, e.g. make an object or
>> run a query, it resolves all the links and everything has to be present.
>>
>>
>>
>>
>> --
>> 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.
>>
>>
>> --
> 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] <javascript:>.
> To post to this group, send email to [email protected]
> <javascript:>.
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.
>
>
>
--
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.