bulk_create always return foreign key constraint fails

59 views
Skip to first unread message

David Bollinger

unread,
Apr 20, 2020, 4:47:35 PM4/20/20
to peewee-orm
HI,

I am trying to do a bulk_create in model having a foreign key constraint, but I always have peewee.IntegrityError:

peewee.IntegrityError: (1452, '1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`orm_test`.`status_esi`, CONSTRAINT `status_esi_ibfk_1` FOREIGN KEY (`device_hostname`) REFERENCES `status_devices` (`device_hostname`))', '23000')

I could not find answers in the documentation (It does not mean it is not there) and I wonder if it is a bug or I'm missing something. Here is my code:


class StatusDevices(BaseModel):
uuid = CharField(default=str(uuid4()), primary_key=True, max_length=36)
device_hostname = CharField(unique=True, max_length=50)
device_ip = CharField(max_length=15)

class Meta:
table_name = 'status_devices'

class StatusEsi(BaseModel):
   uuid = CharField(default=str(uuid4()), primary_key=True, max_length=36)
   device_hostname = ForeignKeyField(
       column_name='device_hostname', field='device_hostname', model=StatusDevices
   )
    class Meta:
       table_name = 'status_esi'


with DATABASE.atomic():
StatusEsi.bulk_create(
[
StatusEsi(
device_hostname='tata', # This hostname exist in status_devices databse
uuid=str(uuid4()),
)
], batch_size=50)


I think everything is there.

Curious enought if I run this query using Model.insert() everything looks ok.

Thanks for any help !

Charles Leifer

unread,
Apr 20, 2020, 6:05:23 PM4/20/20
to peewe...@googlegroups.com
Replicated and created a ticket: https://github.com/coleifer/peewee/issues/2154

--
You received this message because you are subscribed to the Google Groups "peewee-orm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to peewee-orm+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/peewee-orm/8387fbf1-f86c-49aa-a0f8-6967b1ca822c%40googlegroups.com.

David Bollinger

unread,
Apr 21, 2020, 8:09:19 AM4/21/20
to peewee-orm
Thanks Charles!

David Bollinger

unread,
Apr 22, 2020, 9:52:25 AM4/22/20
to peewee-orm
Stupid question but do you know when the next release is planned?


Le lundi 20 avril 2020 16:47:35 UTC-4, David Bollinger a écrit :

Charles Leifer

unread,
Apr 23, 2020, 8:39:29 PM4/23/20
to peewe...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "peewee-orm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to peewee-orm+...@googlegroups.com.

David Bollinger

unread,
Apr 24, 2020, 8:23:45 AM4/24/20
to peewee-orm
Awesome !!


Le jeudi 23 avril 2020 20:39:29 UTC-4, Charles a écrit :
To unsubscribe from this group and stop receiving emails from it, send an email to peewe...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages