【发布时间】:2019-09-17 00:15:26
【问题描述】:
当我尝试通过烧瓶 sqlalchemy 将新行插入表中时收到以下错误。我只在尝试插入新行时看到此错误。我可以毫无问题地查询现有行。
ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'builtin_function_or_method' [SQL: 'INSERT INTO beaches (id, beach_name, lat, long, beach_description) VALUES (%(id)s, %(beach_name)s, %(lat)s, %(long)s, %(beach_description)s)'] [parameters: {'lat': 45.01, 'beach_description': 'some stuff here', 'id': <built-in function id>, 'long': 45.01, 'beach_name': 'test'}] (Background on this error at: http://sqlalche.me/e/f405)
这是我在模型文件中定义“海滩”类的代码。
class Beach(db.Model):
__tablename__ = 'beaches'
id = db.Column(db.Integer, primary_key=True)
beach_name = db.Column(db.String(256))
lat = db.Column(db.Numeric)
long = db.Column(db.Numeric)
beach_description = db.Column(db.String(256))
def __init__(self, id, beach_name, lat, long, beach_description):
self.id = id
self.beach_name = beach_name
self.lat = lat
self.long = long
self.beach_description = beach_description
这是我创建类实例并尝试插入数据库的代码。
beach_name = "test"
lat = 45.01
long = 45.01
beach_description = "some stuff here"
new_beach = Beach(id, beach_name, lat, long, beach_description)
db.session.add(new_beach)
db.session.commit()
这是我要插入的表的架构。我正在使用 Postgres。
CREATE TABLE beaches (
id SERIAL PRIMARY KEY,
beach_name text,
lat double precision,
long double precision,
beach_description text
);
有一个 similar-looking post 评论者建议在创建类的实例时删除“id”。对我来说,这看起来像下面的代码。我已经尝试过这种变化,但我得到了同样的错误。
beach_name = "test"
lat = 45.01
long = 45.01
beach_description = "some stuff here"
new_beach = Beach(beach_name, lat, long, beach_description)
db.session.add(new_beach)
db.session.commit()
即使上述建议不起作用,我认为问题出在我表中的自动递增 id 列的某个地方。感谢您的建议!
【问题讨论】:
标签: postgresql python-2.7 flask-sqlalchemy