Attributeerror: type Object 'User' has no attribute 'order_by' - Sqlalchemy

Asked

Viewed 36 times

0

I am trying to sort the Transaction table id accordingly:

from sqlalchemy import desc
from sqlalchemy.orm import query
from website.models import Transaction


def home():
    transactions = query.order_by(Transaction.id.desc())
    flash(transactions)
    return render_template("home.html", user=current_user)

And also as follows:

from sqlalchemy import desc
from sqlalchemy.orm import query
from website.models import Transaction


def home():
    transactions = Transaction.oder_by(Transaction.id.desc()).all()
    flash(transactions)
    return render_template("home.html", user=current_user)

*The table:

class Transaction(db.Model):
    __tablename__ = 'transaction'

    id = db.Column(db.Integer, primary_key=True)
    tipo = db.Column(db.String(150))
    nome_cripto = db.Column(db.String(150))
    quant = db.Column(db.Float)
    data = db.Column(db.String(150))
    quotation = db.Column(db.Float)

    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

    cripto = db.relationship("Cripto", back_populates="transaction", uselist=False)

But returning, in both attempts: "Attributeerror: type Object 'User' has no attribute 'oder_by'". What is the cause of this? Some import is missing?

1 answer

0

You are giving this Exception because you made a typo by typing "oder_by" instead of "theRder_by".

Since the command does not exist, consequently its object does not have this attribute, generating the problem.

Just fix the typing and it will work as expected.

  • Worse that I made a mistake, but unfortunately even correcting gave the same error.

  • If you are giving the same error, then you are wrong somewhere else in the code. Look straight at the line that appears in the msg of ero

Browser other questions tagged

You are not signed in. Login or sign up in order to post.