Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Peewee KeyError: 'i'

I am getting an odd error from Python's peewee module that I am not able to resolve, any ideas? I basically want to have 'batches' that contain multiple companies within them. I am making a batch instance for each batch and assigning all of the companies within it to that batch's row ID.

Traceback

Traceback (most recent call last):
  File "app.py", line 16, in <module>
    import models
  File "/Users/wyssuser/Desktop/dscraper/models.py", line 10, in <module>
    class Batch(Model):
  File "/Library/Python/2.7/site-packages/peewee.py", line 3647, in __new__
    cls._meta.prepared()
  File "/Library/Python/2.7/site-packages/peewee.py", line 3497, in prepared
    field = self.fields[item.lstrip('-')]
KeyError: 'i'

models.py

from datetime import datetime

from flask.ext.bcrypt import generate_password_hash
from flask.ext.login import UserMixin

from peewee import *

DATABASE = SqliteDatabase('engineering.db')

class Batch(Model):
  initial_contact_date = DateTimeField(formats="%m-%d-%Y")

  class Meta:
    database = DATABASE
    order_by = ('initial_contact_date')

  @classmethod
  def create_batch(cls, initial_contact_date):
    try:
      with DATABASE.transaction():
        cls.create(
          initial_contact_date=datetime.now
          )
        print 'Created batch!'
    except IntegrityError:
      print 'Whoops, there was an error!'


class Company(Model):
  batch_id = ForeignKeyField(rel_model=Batch, related_name='companies')
  company_name = CharField()
  website = CharField(unique=True)
  email_address = CharField()
  scraped_on = DateTimeField(formats="%m-%d-%Y")
  have_contacted = BooleanField(default=False)
  current_pipeline_phase = IntegerField(default=0)

  day_0_message_id = IntegerField()
  day_0_response = IntegerField()
  day_0_sent = DateTimeField()

  day_5_message_id = IntegerField()
  day_5_response = IntegerField()
  day_5_sent = DateTimeField()

  day_35_message_id = IntegerField()
  day_35_response = IntegerField()
  day_35_sent = DateTimeField()

  day_125_message_id = IntegerField()
  day_125_response = IntegerField()
  day_125_sent = DateTimeField()

  sector = CharField()

  class Meta:
    database = DATABASE
    order_by = ('have_contacted', 'current_pipeline_phase')

  @classmethod
  def create_company(cls, company_name, website, email_address):
    try:
      with DATABASE.transaction():
        cls.create(company_name=company_name, website=website, email_address=email_address, scraped_on=datetime.now)
        print 'Saved {}'.format(company_name)
    except IntegrityError:
      print '{} already exists in the database'.format(company_name)


def initialize():
  DATABASE.connect()
  DATABASE.create_tables([Batch, Company, User],safe=True)
  DATABASE.close()
like image 810
Joey Orlando Avatar asked Dec 25 '22 18:12

Joey Orlando


1 Answers

The issue lies within the Metadata for your Batch class. See peewee's example where order_by is used:

class User(BaseModel):
    username = CharField(unique=True)
    password = CharField()
    email = CharField()
    join_date = DateTimeField()

    class Meta:
        order_by = ('username',)

where order_by is a tuple containing only username. In your example you have omitted the comma which makes it a regular string instead of a tuple. This would be the correct version of that part of your code:

class Batch(Model):
    initial_contact_date = DateTimeField(formats="%m-%d-%Y")

    class Meta:
        database = DATABASE
        order_by = ('initial_contact_date',)
like image 130
wonderb0lt Avatar answered Dec 28 '22 09:12

wonderb0lt