Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force strict sql mode in django

Tags:

I'm trying to force my django project to always use strict sql_mode. Is there another way than putting the following in manage.py? It seems overly complicated.

def set_strict_sql_mode(sender, **kwargs):     from django.conf import settings     if settings.DATABASES['default']['ENGINE'] == 'django.db.backends.mysql':         from django.db import connection         cursor = connection.cursor()         cursor.execute('SET session sql_mode=traditional')  from django.core.signals import request_started request_started.connect(set_strict_sql_mode) 
like image 704
jonny Avatar asked Apr 11 '14 21:04

jonny


People also ask

How do I enable strict mode in SQL?

Open the my. ini file. *On the line with "sql_mode", modify the value to turn strict mode ON/OFF. Save the file.

What is SQL strict?

Strict SQL Mode. Strict mode controls how MySQL handles invalid or missing values in data-change statements such as INSERT or UPDATE . A value can be invalid for several reasons. For example, it might have the wrong data type for the column, or it might be out of range.

Can we connect MySQL with Django?

To use MySql as the backend engine for a Django project, we need to follow a simple setup: Install the MySql Server (we can also use a remote one) Install the Mysql Python driver - used by Django to connect and communicate. Create the Mysql database and the user.


2 Answers

Actually asking proved to be a good rubber duck. Just after asking, I found the custom database OPTIONS one can supply in the DATABASES settings like this:

DATABASES = {     'default': {         'ENGINE': 'django.db.backends.mysql',         'OPTIONS': {             'sql_mode': 'traditional',         }     } } 

Hope it helps anyone!

like image 128
jonny Avatar answered Sep 28 '22 04:09

jonny


You can also try with Adding below option in Database []

'OPTIONS': {         'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",     }, 

Its working.

like image 35
Manoj Jadhav Avatar answered Sep 28 '22 05:09

Manoj Jadhav