gpt4 book ai didi

Flask-SQLAlchemy ArgumentError : Object is not legal as a SQL literal value

转载 作者:行者123 更新时间:2023-12-04 12:44:06 30 4
gpt4 key购买 nike

尝试通过 SQL-Alchemy 执行选择查询时,出现以下错误:
ArgumentError: Object <hello.models.User object at 0x106763210> is not legal as a SQL literal value
模型.py

Base = declarative_base()
Base.query = db_session.query_property()

@login_manager.user_loader
def get_user(uid):
user = User.query.filter_by(id=uid).first()
return user
#Where code error ocurrs

class User(Base, UserMixin):
__tablename__ = 'users'

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(120))
email = db.Column(db.String(120), unique=True)
company = db.Column(db.String(120))
password = db.Column(db.String(90))
admin = db.Column(db.Boolean, nullable=False, default=False)

最佳答案

在我的例子中,我执行了一个查询,它有一个对象而不是一个特定的值。
这就是我得到错误的方式。

user=Users.query.filter_by(id=someid).first()#this was not a problem,

但第二个查询就像
friends=Friends.query.filter_by(user).first()#this is where i got ht problem 


flask-sqlalchemy-argumenterror-object-is-not-legal-as-a-sql-literal-value。

这意味着

i am trying to execute a query which has an object users, instead user.id(or something like that which should be a value)



所以当我意识到这一点时,我将查询更改为
Friends=Friends.query.filter_by(user.id).first()#worked fine

关于Flask-SQLAlchemy ArgumentError : Object is not legal as a SQL literal value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49723956/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com