Answer the question
In order to leave comments, you need to log in
How to connect MySQL with Django?
Hello! Please help me figure it out.
I'm trying to connect locally to a MySQL database , I'll say right away that before that I only worked with MS SQL SERVER .
I 'm using:
MySQL Server 5.5
Django 1.10.5
The problem is that the migrate command fails . What to do, where to look, how to fix it?
Here's what I did:
1) Created a database in MySQL on the command line:
CREATE DATABASE `rms` /*!40100 DEFAULT CHARACTER SET utf8 */ ;
pip install mysqlclient
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'rms',
'USER': 'root',
'PASSWORD': '*******',
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
},
}
}
python manage.py migrate
. At this stage, an error is displayed, not all tables are created. After that, I used this command: python manage.py migrate --fake
, which added all the other tables. django.db.utils.ProgrammingError: (1146, "Table 'rms.django_site' doesn't exist")
. I so understand not all tables were created. Repeated migration gives the same error. How to be? What am I doing wrong? Traceback (most recent call last):
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\handlers\exception.py", line 39, in inner
response = get_response(request)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\views\decorators\cache.py", line 57, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\contrib\admin\sites.py", line 377, in login
return login(request, **defaults)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\contrib\auth\views.py", line 47, in inner
return func(*args, **kwargs)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\views\decorators\debug.py", line 76, in sensitive_post_parameters_wrapper
return view(request, *args, **kwargs)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\utils\decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\views\decorators\cache.py", line 57, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\contrib\auth\views.py", line 87, in login
current_site = get_current_site(request)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\contrib\sites\shortcuts.py", line 15, in get_current_site
return Site.objects.get_current(request)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\contrib\sites\models.py", line 65, in get_current
return self._get_site_by_id(site_id)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\contrib\sites\models.py", line 35, in _get_site_by_id
site = self.get(pk=site_id)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\models\query.py", line 379, in get
num = len(clone)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\models\query.py", line 238, in __len__
self._fetch_all()
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\models\query.py", line 1087, in _fetch_all
self._result_cache = list(self.iterator())
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\models\query.py", line 54, in __iter__
results = compiler.execute_sql()
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\models\sql\compiler.py", line 835, in execute_sql
cursor.execute(sql, params)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\backends\utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\utils\six.py", line 685, in reraise
raise value.with_traceback(tb)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\backends\mysql\base.py", line 110, in execute
return self.cursor.execute(query, args)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\MySQLdb\cursors.py", line 250, in execute
self.errorhandler(self, exc, value)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\MySQLdb\connections.py", line 50, in defaulterrorhandler
raise errorvalue
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\MySQLdb\cursors.py", line 247, in execute
res = self._query(query)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\MySQLdb\cursors.py", line 411, in _query
rowcount = self._do_query(q)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\MySQLdb\cursors.py", line 374, in _do_query
db.query(q)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\MySQLdb\connections.py", line 292, in query
_mysql.connection.query(self, query)
django.db.utils.ProgrammingError: (1146, "Table 'rms.django_site' doesn't exist")
Answer the question
In order to leave comments, you need to log in
--fake doesn't add any tables other than django_migrations.
It only writes to the django_migrations table a list of migrations that you are trying to roll into the database, but this does not affect the schema.
If an error is thrown on a clean database that the table does not exist, then the migrations are probably inconsistent.
Use PostgreSQL, you'll still have problems with muscle.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question