2

I've seen other questions with a similar error though their fixes did not solve my issue. Notice that I'm using Flask-SQLAlchemy.

Here is my models file:

class subjects(db.Model):

    id = db.Column(db.Integer, primary_key=True)
    subject = db.Column(db.String(100))

class questions(db.Model):

    id = db.Column(db.Integer, primary_key=True)
    accountID = db.Column(db.Integer, db.ForeignKey('accounts.id'))
    subjectID = db.Column(db.Integer, db.ForeignKey('subjects.id'))
    subject = db.relationship('subjects')
    topic = db.Column(db.String(150), nullable=True)

As you may see, I want questions.subject to refer to subjects.subject. I try to form a query:

topicList = db.session.query(questions.topic.distinct()).filter_by(subject=subjectID, accountID=accountID)

I receive an error stating AttributeError: 'unicode' object has no attribute '_sa_instance_state'

What does this error mean and how to I fix my code to prevent this error? Thanks.

1 Answer 1

2

You are filtering by subject, but providing a string instead of an object. You either need to use an object

subject = db.session.query(subjects).get(subjectID)
topicList = db.session.query(questions.topic.distinct()).filter_by(subject=subject, accountID=accountID)

or you need to query by subjectID

topicList = db.session.query(questions.topic.distinct()).filter_by(subjectID=subjectID, accountID=accountID)

In either case, you appear to be using subjectID as a string when it should be an integer. I'm not sure how it's getting its initial value, but you'll want to address that there.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.