Issue: KeyError / AttributeError
Author: databe
State: closed
Created: 2019-02-18 16:34:45

Hi, I'm using peewee because I'm retrieving data from an API and want to put it into a postgres database. First I tried with 'create()', 'insert()' and 'get_or_create()' and everything worked fine. But now I'm working with a biggest data table and I have to work with 'insert_many()'. Appart of that function, the rest of the code is similar, but adapted to the table in question. With the data a retrieve first, I create a dictionary with all that data and then I try to insert the data using the 'insert_many()' function. This is my code:

```
class Driver(Model):
    SiteId: BigIntegerField(null=True)
    DriverId: IntegerField(null=False)
    Name: CharField(null=True)
    ImageUri: CharField(null=True)
    FmDriverId: IntegerField(null=True)
    EmployeeNumber: CharField(null=True)
    IsSystemDriver: BooleanField(null=True)
    MobileNumber: CharField(null=True)
    Email: CharField(null=True)
    ExtendedDriverId: CharField(null=True)
    ExtendedDriverIdType: CharField(null=True)
    Country: CharField(null=True)

    class Meta:
        database = db
        primary_key = CompositeKey('DriverId')
        db_table = 'tmp_drivers'

def insertoDatos(datos): #the data are correct
    try:
        print('TRY 1')
        db.connect() #creo conexion
        db.create_table([Driver]) #also create_tables([Driver])
    except:
        print('EXCEPT 1')
        print('\nAlgo ha ocurrido al crear la tabla.')
    try:
        print('TRY 2')
        with db.atomic():
            Driver.insert_many(datos).execute()
            db.close()
        print('Datos insertados en la base de datos')
        traceback.print_exc()
    except:
        print('EXCEPT 2')
        print('\nAlgo ha ocurrido al insertar los datos en la base de datos\n')
        traceback.print_exc()
```

using the code above I got this KeyError:

`Traceback (most recent call last):
  File "db_drivers.py", line 113, in insertoDatos
    Driver.insert_many(datos).execute()
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\peewee.py", line 5577, in insert_many
    return ModelInsert(cls, insert=rows, columns=fields)
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\peewee.py", line 6377, in __init__
    self._returning = self.model._meta.get_primary_keys()
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\peewee.py", line 5312, in get_primary_keys
    for field_name in self.primary_key.field_names])
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\peewee.py", line 5312, in <listcomp>
    for field_name in self.primary_key.field_names])
KeyError: 'DriverId'`

But if I change the Model, (I only change the definition of the primary key):

```
class Driver(Model):
    SiteId: BigIntegerField(null=True)
    DriverId: IntegerField(primary_key=True)
    Name: CharField(null=True)
    ImageUri: CharField(null=True)
    FmDriverId: IntegerField(null=True)
    EmployeeNumber: CharField(null=True)
    IsSystemDriver: BooleanField(null=True)
    MobileNumber: CharField(null=True)
    Email: CharField(null=True)
    ExtendedDriverId: CharField(null=True)
    ExtendedDriverIdType: CharField(null=True)
    Country: CharField(null=True)

    class Meta:
        database = db
        #primary_key = CompositeKey('DriverId')
        db_table = 'tmp_drivers'
```

I got this:

`Traceback (most recent call last):
  File "db_drivers.py", line 113, in insertoDatos
    Driver.insert_many(datos).execute()
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\peewee.py", line 1625, in inner
    return method(self, database, *args, **kwargs)
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\peewee.py", line 1696, in execute
    return self._execute(database)
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\peewee.py", line 2358, in _execute
    return super(Insert, self)._execute(database)
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\peewee.py", line 2119, in _execute
    cursor = self.execute_returning(database)
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\peewee.py", line 2126, in execute_returning
    cursor = database.execute(self)
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\playhouse\postgres_ext.py", line 463, in execute
    sql, params = ctx.sql(query).query()
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\peewee.py", line 567, in sql
    return obj.__sql__(self)
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\peewee.py", line 2342, in __sql__
    self._generate_insert(self._insert, ctx)
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\peewee.py", line 2244, in _generate_insert
    column = getattr(self.table, key)
AttributeError: type object 'Driver' has no attribute 'SiteId'`

My environment: W10, python 3.7.2, peewee 3.8.2

I need to understand whats happening so I need help.
Thanks in advance!!

You're using "colon" to separate the parameters? Is this just a typo in the code you've provided? You need equals signs.

Your code:

```python
class Driver(Model):
    SiteId: BigIntegerField(null=True)
    DriverId: IntegerField(null=False)
```

Correct code:

```python
class Driver(Model):
    SiteId = BigIntegerField(null=True)
    DriverId = IntegerField(null=False)
```

Thanks! now it's working fine...my fault!!