Django: How to set foreign key checks to 0
Question: Ok so i'm migrating database from sqlite to mysql , i had few errors but i
already resolved them. Now i have problem with this option because i don't
know how to disable it. I tried

    
    
    DATABASES = {
    
     'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'slave',                     
        'USER': 'root',                     
        'PASSWORD': 'root',                 
        'OPTIONS': {
                    "init_command": "SET foreign_key_checks = 0;",
               },
        'HOST': '',                    
        'PORT': '',                      
       }
    }
    

But it doesn't works and i don't know why.

Ofc i use json files to migration

    
    
    python manage.py dumpdata --indent 2 --natural > dump.json
    python manage.py loaddata dump.json
    

When I'm loading data on begining i can see:

    
    
    SET SQL_AUTO_IS_NULL = 0
    SET foreign_key_checks=0
    

But after some time:

    
    
    SELECT (1) AS `a` FROM `xxx` WHERE `xxx`.`id` = 8  LIMIT 1
    SET foreign_key_checks=1
    

And then i see exception. Traceback isn't important because it is connected
with foreignkeys you can read more here

<http://coffeeonthekeyboard.com/django-fixtures-with-circular-foreign-
keys-480/>

I know that i need to disable this option.

I tried :

<http://djangosaur.tumblr.com/post/7842592399/django-loaddata-mysql-foreign-
key-constraints>

But like i said it doesn't work.

Can someone help...


Answer: you can put this at the end of your `settings.py`:

    
    
    import sys
    if 'loaddata' in sys.argv:
        # only change this for loaddata command.
        DATABASES['default']['OPTIONS'] = {
           "init_command": "SET foreign_key_checks = 0;",
        }
    

