Issue: Bulk insert with uuid foreign key
Author: deissh
State: closed
Created: 2019-07-29 14:04:21

I'm using SQLite with PeeWee 3.9.6
```python3
TrainingMuscleGroups.insert_many(
    [
        *muscle_groups
    ],
    fields=[
        TrainingMuscleGroups.training,
        TrainingMuscleGroups.muscle_group
    ]
).execute()
```

Models

```python3
class MuscleGroups(Model):
    id = UUIDField(default=new_uuid, primary_key=True)
    name = TextField(null=False, unique=True)


class TrainingMuscleGroups(Model):
    id = UUIDField(default=new_uuid, primary_key=True)
    muscle_group = ForeignKeyField(MuscleGroups)

```

will be an error like this

```
tests/test_a9_trainings.py:218: in fill_trainings
    training = TrainingService.create(**data)
app/services/TrainingService.py:50: in create
    TrainingMuscleGroups.muscle_group_id
venv/lib/python3.6/site-packages/peewee.py:1785: in inner
    return method(self, database, *args, **kwargs)
venv/lib/python3.6/site-packages/peewee.py:1856: in execute
    return self._execute(database)
venv/lib/python3.6/site-packages/peewee.py:2572: in _execute
    return super(Insert, self)._execute(database)
venv/lib/python3.6/site-packages/peewee.py:2320: in _execute
    cursor = database.execute(self)
venv/lib/python3.6/site-packages/peewee.py:2962: in execute
    return self.execute_sql(sql, params, commit=commit)
venv/lib/python3.6/site-packages/peewee.py:2953: in execute_sql
    raise
venv/lib/python3.6/site-packages/peewee.py:2732: in __exit__
    reraise(new_type, new_type(*exc_args), traceback)
venv/lib/python3.6/site-packages/peewee.py:183: in reraise
    raise value.with_traceback(tb)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <peewee.SqliteDatabase object at 0x7f7392bc8cc0>
sql = 'INSERT INTO "training_muscle_groups" ("training_id", "muscle_group_id", "id") VALUES (?, ?, ?)'
params = ['f5f44e2f-23e0-4f9f-a677-c157795b5726', 'a46e0c0658394439a6b8e3e63f3e19ac', '065675c39e304ca5b1c0a26fbdba5984']
commit = False

    def execute_sql(self, sql, params=None, commit=SENTINEL):
        logger.debug((sql, params))
        if commit is SENTINEL:
            if self.in_transaction():
                commit = False
            elif self.commit_select:
                commit = True
            else:
                commit = not sql[:6].lower().startswith('select')
    
        with __exception_wrapper__:
            cursor = self.cursor(commit)
            try:
>               cursor.execute(sql, params or ())
E               peewee.IntegrityError: FOREIGN KEY constraint failed

venv/lib/python3.6/site-packages/peewee.py:2949: IntegrityError
```



***some useless information (maybe)***
if I'm using bulk_create like this

```python3
TrainingMuscleGroups.bulk_create([
    TrainingMuscleGroups(
        training=training,
        muscle_group_id=str(muscle_group.id)  # and muscle_group=muscle_group is are eq
    )
    for muscle_group in muscle_groups
])
```

peewee will transform `id` field to UUIDField and finally in [`execute_sql`](https://github.com/coleifer/peewee/blob/3.9.6/peewee.py#L2936) params be like this

![Screenshot_20190729_170330](https://user-images.githubusercontent.com/21129524/62054706-d09b9480-b222-11e9-96f3-516bc4e1dcbc.png)



Sqlite is indicating that you have a foreign-key constraint error. That is, one of the objects you are creating, is using an invalid foreign-key.

Peewee attempts to convert the UUID instances in your models into their hex equivalent for storage, but it looks like one of your parameters is coming through with the dashes in place. Without seeing the "training" field it's hard for me to tell what's going on.

Added a test-case and replicated what I guess to be the bug, which is also fixed in the above commit.

thx
its work