Django mysql doesn't check foreign key constraints

Hello,

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

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:

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.

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.

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

Thank you.