thesayhey
thesayhey

Reputation: 958

Query/Filter method resulting in NameError

I have a table with two (M-1) relationships with respective ForeignKeys. I want to verify that ids match. I attempted to compare ids and am receiving: NameError: global name 'owner_id' is not defined

I am uncertain why this is the issue. I wasn't sure if I should use a join method per SQLAlchemy docs, but it is within the table. Calling self. also doesn't work.

The table is:

class Assessment_Results(Base):
            __tablename__ = 'assessment_results'

            id = Column(Integer, primary_key=True)
            created_on = Column(DateTime, default=datetime.utcnow)

            owner_id = Column(Integer, ForeignKey('users.user_id'))
            owner = relationship('User', backref='assessment_results')

            assessment_id = Column(Integer, ForeignKey('assessments.assessment_id'))
            assessment = relationship('Assessment', backref='assessment_results')

            def __init__(self, owner, assessment):
                self.owner = owner
                self.assessment = assessment 

The query:

def retrieve_assessment_results(self, owner, assessment): 
    assess_results = self.session.query(Assessment_Results).\
    filter(Assessment_Results.id == owner_id).\
    filter(Assessment_Results.id == assessment_id).all()
    return assess_results

Entire Traceback:

ERROR: notssdb.test.test.test1
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/Users/ack/code/venv/NotssDB/notssdb/test/test.py", line 86, in test1
    api.create_category_rating(2, 'Decision-Making', 'baseball', 'Becoming a Leader')
  File "/Users/ack/code/venv/NotssDB/notssdb/api/convenience.py", line 29, in create_category_rating
    assessment_results = self.retrieve_assessment_results(owner, assessment)
  File "/Users/ack/code/venv/NotssDB/notssdb/api/object.py", line 273, in retrieve_assessment_results
    filter(Assessment_Results.id == owner_id).\
NameError: global name 'owner_id' is not defined

New Traceback Error after this new implementation:

def retrieve_assessment_results(self, owner, assessment): 
    assess_results = self.session.query(Assessment_Results).\
    filter(Assessment_Results.owner_id == owner.id).\
    filter(Assessment_Results.assessment_id == assessment.id).all()
    return assess_results

Inheritance class of the method above:

def create_assessment_results(self, username, name):
    owner = self.retrieve_user(username)
    assessment = self.retrieve_assessment(name)
    return super(ConvenienceAPI, self).create_assessment_results(owner, assessment)

Here is cat_rating :

def create_category_rating(self, category_rating_int, category_name, owner, assessment):
    category = self.retrieve_category(category_name)
    assessment_results = self.retrieve_assessment_results(owner, assessment)
    return super(ConvenienceAPI, self).create_category_rating(category_rating_int, category, assessment_results)    

Error:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/Users/ack/code/venv/NotssDB/notssdb/test/test.py", line 86, in test1
    api.create_category_rating(2, 'Decision-Making', 'baseball', 'Becoming a Leader')
  File "/Users/ack/code/venv/NotssDB/notssdb/api/convenience.py", line 29, in create_category_rating
    assessment_results = self.retrieve_assessment_results(owner, assessment)
  File "/Users/ack/code/venv/NotssDB/notssdb/api/object.py", line 272, in retrieve_assessment_results
    filter(Assessment_Results.owner_id == owner.id).\
AttributeError: 'str' object has no attribute 'id'

Upvotes: 0

Views: 425

Answers (1)

Chad S.
Chad S.

Reputation: 6633

The problem is this line right here..

filter(Assessment_Results.id == owner_id).\

owner_id was not defined in this method and is not being accessed as an attribute of an Assessment_Results object, so the interpreter doesn't know what object you are trying to refer to by this name.

I think you probably want to do

filter(Assessment_Results.owner_id == owner.id).\

but that assumes that owner is an instance of the owner model.

You'll end up having the same problem with assessment_id (because it is also not defined within the method) once you figure out what variable you meant to use for owner_id.

Upvotes: 1

Related Questions