【问题标题】:Unable to Insert New Records via Flask SQLAlchemy无法通过 Flask SQLAlchemy 插入新记录
【发布时间】: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


    【解决方案1】:

    如果有人遇到类似的问题...

    解决方案是从参数列表中删除“id”并从 Beach 类中删除 self.id = id 位,并在创建 Beach 类的新实例时删除“id”。

    我猜由于 beachs 表配置为在添加新记录时自动增加 id(通过串行数据类型),因此在创建类的新实例时不需要提供 id。

    修改后的类代码如下所示。

    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, beach_name, lat, long, beach_description):
            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(beach_name, lat, long, beach_description)
    
    db.session.add(new_beach)
    db.session.commit()
    

    【讨论】:

      猜你喜欢
      • 2023-03-18
      • 2017-12-15
      • 2010-11-13
      • 1970-01-01
      • 2016-10-01
      • 2018-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多