On 7/1/14, 1:17 PM, trusted...@gmail.com 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 sqlalchemy+unsubscr...@googlegroups.com > <mailto:sqlalchemy+unsubscr...@googlegroups.com>. > To post to this group, send email to sqlalchemy@googlegroups.com > <mailto:sqlalchemy@googlegroups.com>. > 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 sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.