Python KeyError:<peewee.integerfield object =“”at =“”... =“”>使用peewee insert_many()

时间:2017-05-07 15:19:17

标签: python postgresql peewee socrata

作为练习,我从API中提取数据并将其插入到psql数据库中。我最初遵循每次拉入1000个条目的默认限制,但我决定尝试获取大约40K行的所有数据。经过一些实验,我可以拉4800,但接着我得到以下内容:

Traceback (most recent call last):
  File "data_pull.py", line 19, in <module>
    postgres_db.Bike_Count.insert_many(data).execute()
  File "/usr/local/lib/python3.5/dist-packages/peewee.py", line 3516, in execute
    cursor = self._execute()
  File "/usr/local/lib/python3.5/dist-packages/peewee.py", line 2901, in _execute
    sql, params = self.sql()
  File "/usr/local/lib/python3.5/dist-packages/peewee.py", line 3484, in sql
    return self.compiler().generate_insert(self)
  File "/usr/local/lib/python3.5/dist-packages/peewee.py", line 2084, in generate_insert
    value = row_dict[field]
KeyError: <peewee.IntegerField object at 0x7f5b32c2c7f0>

data_pull.py

import json, requests, peewee
import postgres_db


endpoint =  'https://data.seattle.gov/resource/4xy5-26gy.json?$limit=4800'

response = requests.get(endpoint, headers={'X-App-Token': '(REMOVED)'})
if response.status_code == 200:
    data = json.loads(response.text)


postgres_db.Bike_Count.create_table(True)
postgres_db.Bike_Count.insert_many(data).execute()

postgres_db.py

import peewee


psql_db = peewee.PostgresqlDatabase('database', user='my_username')

class Bike_Count(peewee.Model):
    date = peewee.DateTimeField()
    fremont_bridge_sb = peewee.IntegerField()
    fremont_bridge_nb = peewee.IntegerField()

    class Meta:
        database = psql_db

我在网上查看了表格,认为在那里有一个条目存在问题,但我找不到任何明显的东西。谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

我在本地尝试了您的代码(删除了应用令牌和4800限制)并且按预期工作:

  id  |        date         | fremont_bridge_sb | fremont_bridge_nb 
------+---------------------+-------------------+-------------------
    1 | 2017-01-09 06:00:00 |                28 |                55
    2 | 2017-01-04 20:00:00 |                19 |                10
    3 | 2017-01-18 13:00:00 |                18 |                18
    4 | 2017-01-06 11:00:00 |                22 |                15
    5 | 2017-01-27 11:00:00 |                39 |                38
    6 | 2017-01-08 14:00:00 |                 6 |                10
    7 | 2017-01-06 23:00:00 |                 8 |                 3
    8 | 2017-01-27 13:00:00 |                45 |                35
...

我在附加LIMIT时运行它时注意到,API返回的其中一行只包含一个date键(缺少fremont_bridge_nb和fremont_bridge_sb字段)。

Peewee要求批量插入每行具有相同的键,因此问题是peewee期望找到所有3个键。