Issue: Columns disappear on INSERT, UPDATE with UUID foreign key
Author: kennethklee
State: closed
Created: 2015-09-26 22:23:09

Cannot insert, update the uuid foreign key with this model:

``` python
class UnitRevision(BaseModel):
    id = UUIDField(primary_key=True, default=uuid.uuid4, null=False)
    user = ForeignKeyField(User, null=False) # uuid relationship
    created_at = DateTimeField(default=datetime.now, null=False, verbose_name='Created At')
    name = CharField(max_length=255, null=False)
    config = TextField(null=False)

# Instantiate
unit = UnitRevision(user=user, name='test', config='test')  # user is an existing user with a uuid primary key
```

Here's the error I get when trying `create()`:

```
*** IntegrityError: null value in column "user_id" violates not-null constraint
DETAIL:  Failing row contains (5844081c-d4e2-4a23-8240-7246bf2474a7, null, 2015-09-26 18:09:26.787624+00, null, null).
```

I get the following when I call `insert()`:

```
<class 'app.database.UnitRevision'> INSERT INTO "unitrevision" ("id", "created_at") VALUES (%s, %s) RETURNING "id" ['17daa1fa-b01a-45cf-a8c9-fe78c0a7f4ea', datetime.datetime(2015, 9, 26, 18, 10, 25, 15981)]
```

What happened to the columns, `user_id`, `name`, and `config`? Why aren't they present?


Should note that updates work except when I change the `user` field. Column seems to just, disappear from the query.


That's bizarre. What version of peewee are you using? Can you put together a small snippet so I can replicate the bug?


peewee==2.6.4

Requires postgres, with a db and user already created. I haven't tried with sqlite, though I'm not sure if sqlite has uuid.

Below is the bug:

```
import os
import uuid

from datetime import datetime
from peewee import PostgresqlDatabase, Proxy, Model, UUIDField, CharField, BooleanField, DateTimeField, TextField, ForeignKeyField

session = PostgresqlDatabase(
    'testdb',
    host='localhost',
    port='5432',
    user='postgres',
    password='postgres'
)

class BaseModel(Model):
    class Meta:
        database = session

class User(BaseModel):
    id = UUIDField(primary_key=True, default=uuid.uuid4, null=False)
    created_at = DateTimeField(default=datetime.now, null=False, verbose_name='Created At')
    active = BooleanField(default=True, null=False, verbose_name='Active')
    name = CharField(max_length=255, null=False)

class UnitRevision(BaseModel):
    id = UUIDField(primary_key=True, default=uuid.uuid4, null=False)
    user = ForeignKeyField(User, null=False) # uuid relationship
    created_at = DateTimeField(default=datetime.now, null=False, verbose_name='Created At')
    name = CharField(max_length=255, null=False)
    config = TextField(null=False)

user = User()
user.create(name='Test')

rev = UnitRevision(user=user, name='test', config='test')
print(rev.insert()) # see odd output
rev.create() # fails
```

Forgot to mention, you might want to do `User.create_table()` and `UnitRevision.create_table()` sometime before creating the users.


I should have read this more carefully. You've got your APIs mixed up. `create()` is a classmethod that accepts `kwargs` of field->value. `insert()` is also a classmethod. You should call `save()` on instances you've built up programmatically.

Rewritten:

``` python
user = User.create(name='Test')

rev = UnitRevision(user=user, name='test', config='test')
rev.save(force_insert=True)
```

Note that when using UUIDs or other "non integer primary keys" you need to save with `force_insert=True`: http://docs.peewee-orm.com/en/latest/peewee/models.html#id3


Thanks, that works.