# Django mysql doesn't check foreign key constraints

**URL:** <https://forum.codewithmosh.com/t/django-mysql-doesnt-check-foreign-key-constraints/8616>\
**Category:** Django\
**Created:** [November 19, 2021, 9:21am UTC](https://forum.codewithmosh.com/t/django-mysql-doesnt-check-foreign-key-constraints/8616 "2021-11-19T09:21:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![koppandi.alin](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/koppandi.alin/32/2603_2.png) [@koppandi.alin](https://forum.codewithmosh.com/u/koppandi.alin)\
**Post date:** [November 19, 2021, 9:21am UTC](https://forum.codewithmosh.com/t/django-mysql-doesnt-check-foreign-key-constraints/8616/1 "2021-11-19T09:21:02Z")

</div>

Hello,

I have a weird situation, I am at lesson 25 - Transactions (Django ORM) and I tried this code

```auto
with transaction.atomic():
        order = Order()
        order.customer_id = -1
        order.save()

        item = OrderItem()
        item.order = order
        item.product_id = 2012
        item.quantity = 1
        item.unit_price = 10
        item.save()

```

in the video I see that Mosh got an IntegrityError ( Cannot add or update a child row: a foreign key constraint fails ). I am using mysql-connector-python instead of mysqlclient , I have to use this because I have a managed VPS where I cannot install mysqlclient ( it depends on 2 other linux libraries and I am not able to install those libraries ). My DB settings looks like this:

```auto
DATABASES = {
    'default': {
        'ENGINE': 'mysql.connector.django',
        'NAME': 'storefront',
        'USER': 'root',
        'PASSWORD': '',
        'HOST': '127.0.0.1',
        'PORT': '3306',
        'OPTIONS': {
            'use_pure': True,
        }
    }
}

```

MySQL db collation is utf8\_general\_ci.

So the problem is that my code runs without any error and inserts and order with customer\_id -1 which doesn’t exists and an orderItem with product\_id 2012 which also doesn’t exists.

I use the same model like Mosh used in his lesson.

I would appreciate any help regarding this weird situation.

Thank you.

---

<div class="post-metadata">

**Author:** ![koppandi.alin](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/koppandi.alin/32/2603_2.png) [@koppandi.alin](https://forum.codewithmosh.com/u/koppandi.alin)\
**Post date:** [November 19, 2021, 2:46pm UTC](https://forum.codewithmosh.com/t/django-mysql-doesnt-check-foreign-key-constraints/8616/2 "2021-11-19T14:46:33Z")

</div>

After spending some hours researching this problem, I found out that the Storage Engine was causing this problem. For those who are using MySQL or MariaDB with WAMP server, the default settings for storage engine is MyISAM which instead of creating a relation based on the foreign key it creates an Index and that’s why it doesn’t care if that Index doesn’t exist in the Related table.

So the solution would be to edit the mysql/mariadb conf file and change the default storage engine to InnoDB than everything will work just fine.

```auto
; The default storage engine that will be used when create new tables
default-storage-engine=InnoDB

```

Thank you.
