Re: [sqlalchemy] Weird NameError: name is not defined - can not establish relationship

Fri, 26 Oct 2018 06:06:22 -0700

On Fri, Oct 26, 2018 at 6:18 AM 'yuri' via sqlalchemy
<[email protected]> wrote:
>
> Hi, I've got strange problem - I have funktioning model with several 
> relationshps. I wanted to add additional table 'Account' that already existed 
> in the database and create one-to-one relationship to User. Indendently both 
> ot the classes work and can be queried, however if I'm trying to establish 
> relationship between them I get following error:
>
> NameError: name 'Account' is not defined
>
> Are there any restrictions dealing with connections


the problem is the "Base" you are passing to Account I assume is
another declarative_base, and it is preventing Flask's db.Model from
accounting for this class.

Is there a reason you are using two separate declarative bases?  using
them together on one class is not supported (or necessary).

>
> My classes are:
> class User(UserMixin, db.Model):
>     id = db.Column(db.Integer, primary_key=True)
>     username = db.Column(db.String(64), index=True, unique=True)
>     email = db.Column(db.String(120), index=True, unique=True)
>     password_hash = db.Column(db.String(128))
>     last_message_read_time = db.Column(db.DateTime)
>     notifications = db.relationship('Notification', backref='user',
>                                     lazy='dynamic')
>     messages_sent = db.relationship('Message',
>                                     foreign_keys='Message.sender_id',
>                                     backref='author', lazy='dynamic')
>     account = db.relationship('Account', uselist=False, back_populates="user")
>
>
> class Account(Base, db.Model):
>     __tablename__ = 'account'
>     __table_args__ = {'schema': 'salesforce'}
>
>     external_registration_code__c = db.Column(db.String(12))
>     name = db.Column(db.String(255))
>     isdeleted = db.Column(db.Boolean)
>     systemmodstamp = db.Column(db.DateTime, index=True)
>     heroku_login__c = db.Column(db.Boolean)
>     createddate = db.Column(db.DateTime)
>     type = db.Column(db.String(40))
>     website = db.Column(db.String(255))
>     firstname = db.Column(db.String(40))
>     description = db.Column(db.Text)
>     sfid = db.Column(db.String(18), unique=True)
>     id = db.Column(db.Integer, primary_key=True, 
> server_default=db.text("nextval('salesforce.account_id_seq'::regclass)"))
>     _hc_lastop = db.Column(db.String(32))
>     _hc_err = db.Column(db.Text)
>     personemail = db.Column(db.String(80))
>     recordtypeid = db.Column(db.String(18))
>     lastname = db.Column(db.String(80))
>     persontitle = db.Column(db.String(80))
>     custom_user__c = db.Column(db.Integer, db.ForeignKey('user.id'))
>     user = db.relationship('User', back_populates='account')
>
> Traceback (most recent call last):
>   File "<console>", line 1, in <module>
>   File 
> "/Users/ygavrylenko/dev/capp/venv/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py",
>  line 512, in __get__
>     mapper = orm.class_mapper(type)
>   File 
> "/Users/ygavrylenko/dev/capp/venv/lib/python3.7/site-packages/sqlalchemy/orm/base.py",
>  line 421, in class_mapper
>     mapper = _inspect_mapped_class(class_, configure=configure)
>   File 
> "/Users/usr/heroku/capp/venv/lib/python3.7/site-packages/sqlalchemy/orm/base.py",
>  line 400, in _inspect_mapped_class
>     mapper._configure_all()
>   File 
> "/Users/usr/heroku/capp/venv/lib/python3.7/site-packages/sqlalchemy/orm/mapper.py",
>  line 1276, in _configure_all
>     configure_mappers()
>   File 
> "/Users/usr/heroku/capp/venv/lib/python3.7/site-packages/sqlalchemy/orm/mapper.py",
>  line 3033, in configure_mappers
>     mapper._post_configure_properties()
>   File 
> "/Users/usr/heroku/capp/venv/lib/python3.7/site-packages/sqlalchemy/orm/mapper.py",
>  line 1832, in _post_configure_properties
>     prop.init()
>   File 
> "/Users/usr/heroku/capp/venv/lib/python3.7/site-packages/sqlalchemy/orm/interfaces.py",
>  line 183, in init
>     self.do_init()
>   File 
> "/Users/usr/dev/capp/venv/lib/python3.7/site-packages/sqlalchemy/orm/relationships.py",
>  line 1655, in do_init
>     self._process_dependent_arguments()
>   File 
> "/Users/usr/dev/capp/venv/lib/python3.7/site-packages/sqlalchemy/orm/relationships.py",
>  line 1712, in _process_dependent_arguments
>     self.target = self.mapper.mapped_table
>   File 
> "/Users/usr/dev/capp/venv/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py",
>  line 767, in __get__
>     obj.__dict__[self.__name__] = result = self.fget(obj)
>   File 
> "/Users/usr/dev/capp/venv/lib/python3.7/site-packages/sqlalchemy/orm/relationships.py",
>  line 1628, in mapper
>     argument = self.argument()
>   File 
> "/Users/usr/dev/capp/venv/lib/python3.7/site-packages/sqlalchemy/ext/declarative/clsregistry.py",
>  line 293, in __call__
>     (self.prop.parent, self.arg, n.args[0], self.cls)
> sqlalchemy.exc.InvalidRequestError: When initializing mapper 
> Mapper|User|user, expression 'Account' failed to locate a name ("name 
> 'Account' is not defined"). If this is a class name, consider adding this 
> relationship() to the <class 'app.models.User'> class after both dependent 
> classes have been defined.
>
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and 
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full 
> description.
> ---
> 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 https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
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 https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to