gpt4 book ai didi

SQLAlchemy 无法适应类型 "builtin_function_or_method"

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

我将 Celery 与 Flask 和 SQLAlchemy 一起使用将一些字符串存储到数据库中。一切正常,但是当我创建数据库类时:

class Locations(db.Model):
id = db.Column('id', db.Integer, primary_key=True)
data = db.Column('data', db.String(50))

def insert():
location="Madrid"
l = Locations(id=id, data=location)
db.session.add(l)
db.session.commit()

当我访问一个 url 页面调用方法 insert() 时,我收到这个错误,我认为这与 id 有关系:
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'builtin_function_or_method' [SQL: 'INSERT INTO locations (id, data) VALUES (%(id)s, %(data)s)'] [parameters: {'id': <built-in function id>, 'data': 'Oslo'}] (Background on this error at: http://sqlalche.me/e/f405)

最佳答案

摆脱类型“id”和“数据”,一切都应该按预期工作。

class Locations(db.Model):
id = db.Column(db.Integer, primary_key=True)
data = db.Column(db.String(50))

def insert():
location="Madrid"
l = Locations(id=id, data=location) # You may want to remove id as well here as it will be autoincrement by default.
# So it can be l=Locations(data=location)
db.session.add(l)
db.session.commit()

关于SQLAlchemy 无法适应类型 "builtin_function_or_method",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52251809/

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