Unable to migrate models from django to sql server - python

I'm trying to learn django and have to use sql server as a data base and unable to migrate models using manage.py migrate
I'm using django 2.1.8
Pyodbc
python 3.7.3
I've tried to reinstall django, python and even sql server but it didn't solve the problem.
On running py manage.py migrate, i get the following error
py manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, testApp
Running migrations:
Applying contenttypes.0001_initial...Traceback (most recent call last):
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sql_server\pyodbc\base.py", line 546, in execute
return self.cursor.execute(sql, params)
pyodbc.ProgrammingError: ('42S02', '[42S02] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot find the object "django_content_type" because it does not exist or you do not have permissions. (4902) (SQLExecDirectW)')
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 353, in execute
output = self.handle(*args, **options)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\commands\migrate.py", line 203, in handle
fake_initial=fake_initial,
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\operations\models.py", line 514, in database_forwards
getattr(new_model._meta, self.option_name, set()),
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\schema.py", line 360, in alter_unique_together
self.execute(self._create_unique_sql(model, columns))
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sql_server\pyodbc\schema.py", line 653, in execute
cursor.execute(sql, params)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 100, in execute
return super().execute(sql, params)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sql_server\pyodbc\base.py", line 546, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: ('42S02', '[42S02] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot find the object "django_content_type" because it does not exist or you do not have permissions. (4902) (SQLExecDirectW)')
I expect it to migrate but it gives me this error.

Might be a different problem, but I was having the same error. In my case I had run python manage.py migrate --fake (per other SO threads) to fix an error I had earlier and django made a record of the migration without actually creating the table. When I ran it again it assumed I was modifying a table that wasn't there.
I eventually solved this by:
Manually creating the table in SQL server with all of the correct fields
Commenting out the model code from everywhere in the models.py
python manage.py makemigrations to remove the fake table from registry
python manage.py migrate to delete the manual table I just made and update the migrations registry
then re-add the table to my models.py
then re-run python manage.py makemigrations
python manage.py migrate to sync django with SQL server

Related

django.db.utils.ProgrammingError: relation "app_bugs" already exists

After I deleted all the migration files and made them again using "python3 manage.py makemigrations" and "python3 manage.py migrate", the following error occurred:
root#chat-manager:/var/www/jinabot# python3 manage.py migrate
Operations to perform:
Apply all migrations: admin, app, auth, contenttypes, sessions
Running migrations:
Applying app.0002_auto_20220604_2133...Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/django/db/backends/utils.py", line 82, in _execute
return self.cursor.execute(sql)
psycopg2.errors.DuplicateTable: relation "app_bugs" already exists
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 23, in <module>
main()
File "manage.py", line 19, in main
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.7/dist-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.7/dist-packages/django/core/management/__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.7/dist-packages/django/core/management/base.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.7/dist-packages/django/core/management/base.py", line 398, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.7/dist-packages/django/core/management/base.py", line 89, in wrapped
res = handle_func(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/django/core/management/commands/migrate.py", line 246, in handle
fake_initial=fake_initial,
File "/usr/local/lib/python3.7/dist-packages/django/db/migrations/executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python3.7/dist-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python3.7/dist-packages/django/db/migrations/executor.py", line 227, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/local/lib/python3.7/dist-packages/django/db/migrations/migration.py", line 126, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/usr/local/lib/python3.7/dist-packages/django/db/migrations/operations/models.py", line 92, in database_forwards
schema_editor.create_model(model)
File "/usr/local/lib/python3.7/dist-packages/django/db/backends/base/schema.py", line 331, in create_model
self.execute(sql, params or None)
File "/usr/local/lib/python3.7/dist-packages/django/db/backends/base/schema.py", line 145, in execute
cursor.execute(sql, params)
File "/usr/local/lib/python3.7/dist-packages/django/db/backends/utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/usr/local/lib/python3.7/dist-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/usr/local/lib/python3.7/dist-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.7/dist-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.7/dist-packages/django/db/backends/utils.py", line 82, in _execute
return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "app_bugs" already exists
How to fix it?
..................................................................
In database, the relation has already been created. You might have two references for bugs relation in your django app models.py file. If you find multiple reference please rename it differently.
If it's isn't the case you can resolve the error in 2 days:
Delete database entirely. If you're using sqlite3 db then just delete db.sqlite3 file present in your project directory.
Delete all migrations file entirely and recreate and apply it.

Django Misapplying Migrations

I am not sure whether I am doing something wrong or it is a problem with one of the pieces I am using for the project.
Basically, I added a field to a model and am trying to make a migration.
Here is the model. The field is the poster one.
class Video(models.Model):
title=models.CharField(max_length=500)
description=models.TextField(default="")
creation_date=models.DateTimeField(default=timezone.now)
videofile=models.FileField(upload_to='videos/', null=True, verbose_name="")
poster=models.ImageField(upload_to='video/thumbnails', null=True, verbose_name="")
tags = TaggableManager()
actions = ['delete']
def __str__(self):
return self.title + ": " + str(self.videofile)
...
That is the only thing that changed in the model. Let's make the migrations.
(app-web) selfishman#user-desktop:~/sites/app-web/app$ python manage.py makemigrations
Migrations for 'video_uploader':
video_uploader/migrations/0007_video_poster.py
- Add field poster to video
So far, so good. Let's try to apply the migration.
(app-web) user#user-desktop:~/sites/app-web/app$ python manage.py migrate video_uploader
Operations to perform:
Apply all migrations: video_uploader
Running migrations:
Applying video_uploader.0002_video_creation_date...Traceback (most recent call last):
File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/db/backends/ut
ils.py", line 85, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.DuplicateColumn: column "creation_date" of relation "video_uploader_video" already exists
There rest of the backtrace:
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 20, in <module>
execute_from_command_line(sys.argv)
File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/core/managemen
t/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/core/managemen
t/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/core/managemen
t/base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/core/managemen
t/base.py", line 353, in execute
output = self.handle(*args, **options)
File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/core/managemen
t/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/core/managemen
t/commands/migrate.py", line 203, in handle
fake_initial=fake_initial,
File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/db/migrations/
executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/db/migrations/
executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/db/migrations/
executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/db/migrations/
migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/db/migrations/
operations/fields.py", line 84, in database_forwards
field,
File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/db/backends/ba
se/schema.py", line 435, in add_field
self.execute(sql, params)
File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/db/backends/ba
se/schema.py", line 133, in execute
cursor.execute(sql, params)
File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/db/backends/ut
ils.py", line 100, in execute
return super().execute(sql, params)
File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/db/backends/ut
ils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/db/backends/ut
ils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/db/backends/ut
ils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/db/utils.py",
line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/db/backends/ut
ils.py", line 85, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column "creation_date" of relation "video_uploader_video" already exists
This is the migration that was created:
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('video_uploader', '0006_video_description'),
]
operations = [
migrations.AddField(
model_name='video',
name='poster',
field=models.ImageField(null=True, upload_to='video/thumbnails', verbose_name=''),
),
]
Could someone tell me what is going on here? I am using Postgres 12. When I run tests, and an (SQLite) DB is created from scratch, there is no such error.
Any help is appreciated.
P.S. We have seen quite a few inconsistencies when it comes to Django migrations and Postgres/Psycopg2. Not sure if something is up with the config or versions/dependencies.
You created a new migration and it was named
0007_video_poster
However when you run the migrate it is running
0002_video_creation_date
And this is trying to create a new column named creation_date however that's already there.
You are getting inconsistent results because django thinks the previous migrations were not applied and therefore it is trying to apply them.
The easiest way would be to flush the database (make sure you first export any data you might need) using
python manage.py flush
This would reset the database and then you can run the migrations normally and it should work fine.
Otherwise if you want to execute the migration you just created i.e. 0007_video_poster
You can run this
python manage.py migrate video_uploader 0007_video_poster

Django Heroku users_customuser table can't migrate

I have a Django project I've been developing locally for a while. It works fine on my local machine, and I am using a customuser setup so that my login auth only needs email / password, as opposed to the default Django user table which needs email / username / password.
I just got my project hosted on Heroku, and can see the webpage UI get loaded fine. But I'm getting Django errors when clicking any links, and think its connected to an issue when I try to run
heroku run python manage.py migrate
I'm trying to run this on heroku to setup the DB, but when I do so I get an error saying:
Running python manage.py migrate on ⬢ songsweeper... up, run.1353 (Free)
Operations to perform:
Apply all migrations: account, admin, auth, contenttypes, playlist, sessions, sites
Running migrations:
Applying account.0001_initial...Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedTable: relation "users_customuser" does not exist
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 234, in handle
fake_initial=fake_initial,
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 247, in apply_migration
migration_recorded = True
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 110, in __exit__
self.execute(sql)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 137, in execute
cursor.execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 99, in execute
return super().execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "users_customuser" does not exist
My project is very large, but to give an overview of where I'm defining customuser, it appears in these places:
Does anyone have any advice on how to fix this migration error? Or is it indicative or something alrger? The 'sign-in' link on my site leads to a different error saying "relation "django_site" does not exist", but I think it has something to do with this failed customuser error.
I am currently using the default sqlite3 db.
I faced the problem similar to yours before.
In my opinion, I think it is because of a migration file in the user app.
So you will have to do migrate the user model.
$ python manage.py makemigrations users
$ python manage.py migrate
I resolved the problem using the above way.

Duplicate column name when migrating Django database

I am trying to migrate my database:
E:\PhytonProgects\natarelke>python manage.py migrate
System check identified some issues:
WARNINGS:
?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'
HINT: MySQL's Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It is strongly recommended
you activate it. See:
https://docs.djangoproject.com/en/1.10/ref/databases/#mysql-sql-mode
Operations to perform:
Apply all migrations: admin, auth, catalog, contenttypes, main, ordering, registration, sessions, users
Running migrations:
Rendering model states... DONE
Applying catalog.0002_auto_20170219_2146...Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
utility.execute()
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 305, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 356, in execute
output = self.handle(*args, **options)
File "C:\Python27\lib\site-packages\django\core\management\commands\migrate.py", line 202, in handle
targets, plan, fake=fake, fake_initial=fake_initial
File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 97, in migrate
state = self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 132, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 237, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Python27\lib\site-packages\django\db\migrations\migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:\Python27\lib\site-packages\django\db\migrations\operations\fields.py", line 84, in database_forwards
field,
File "C:\Python27\lib\site-packages\django\db\backends\mysql\schema.py", line 43, in add_field
super(DatabaseSchemaEditor, self).add_field(model, field)
File "C:\Python27\lib\site-packages\django\db\backends\base\schema.py", line 409, in add_field
self.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\base\schema.py", line 112, in execute
cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\mysql\base.py", line 112, in execute
return self.cursor.execute(query, args)
File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
django.db.utils.OperationalError: (1060, "Duplicate column name 'user_id'")
When I try to run python manage.py migrate I got errors shown above. Can anybody help me fix them?
It's a simple issue which I also faced.
It's nothing big just an issue of migrate command. Usually, when you make new models or new tables, you re-run old migrations along with the newly created ones. Of which sometimes the Django confuses I suppose over here.
Easiest solution what I found was.
1st empty your recycle bin.
then go to your migrations folder or folders( in case you have more than one app)
temporarily delete ( send all migration files such as 0001_initial.py 0002_auto_20170621_1006 etc. all of them to recycle bin.
Then re-run the commands python manage.py makemigrations and python manage.py migrate
*step 3: is temporary del. here you can also go back to recycle bin easily restore it in one click. (so del is safe)
I want to tell you my case. This only works if you be patient and you are sure about the changes.
Django 2.1
{% for crash in crashes %}
python manage.py makemigrations
Locate the conflict file (In the question catalog.0002_auto_20170219_2146.py or something)
Remove (copy in other file or don't close the file) temporally the columns that already exists into the database . (This is what you must be patient :/ ).
{% endfor %}
python manage.py migrate
Undo the change in files.
That's all, works for me.
Moral: if you found an conflict with migrate, you should fix it, dont remove all files.
There is a problem either in your DB, or your migrations, or both. If this is a new project, deleting the DB and all migrations, and recreating them with makemigrations will probably get you out of trouble.
To save your data, you can try reverting to a models.py that matches your DB, and use dumpdata to export your data to a JSON file.

exception messages: What does it mean? - django 1.8 python 3.4 - python manage.py makemigrate

This is the error message: Not sure what is the exception message mean. Plus I don't seem to get any error while running the local server. So is it going to throw any error in the future?
(proj1) C:\Users\vetri\Google Drive\proj1\src>python manage.py mak
emigrations
No changes detected
(proj1) C:\Users\vetri\Google Drive\proj1\src>python manage.py mig
rate
Operations to perform:
Synchronize unmigrated apps: messages, staticfiles
Apply all migrations: contenttypes, sessions, admin, Index, auth
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying Index.0001_initial...Traceback (most recent call last):
File "C:\Users\vetri\Google Drive\proj1\lib\site-packages\django\db\bac
kends\utils.py", line 62, in execute
return self.cursor.execute(sql)
File "C:\Users\vetri\Google Drive\proj1\lib\site-packages\django\db\bac
kends\sqlite3\base.py", line 316, in execute
return Database.Cursor.execute(self, query)
sqlite3.OperationalError: table "Index_usermanagement" already exists
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\vetri\Google Drive\proj1\lib\site-packages\django\core\m
anagement\__init__.py", line 338, in execute_from_command_line
utility.execute()
File "C:\Users\vetri\Google Drive\proj1\lib\site-packages\django\core\m
anagement\__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\vetri\Google Drive\proj1\lib\site-packages\django\core\m
anagement\base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\vetri\Google Drive\proj1\lib\site-packages\django\core\m
anagement\base.py", line 441, in execute
output = self.handle(*args, **options)
File "C:\Users\vetri\Google Drive\proj1\lib\site-packages\django\core\m
anagement\commands\migrate.py", line 221, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "C:\Users\vetri\Google Drive\proj1\lib\site-packages\django\db\mig
rations\executor.py", line 110, in migrate
self.apply_migration(states[migration], migration, fake=fake, fake_initial=f
ake_initial)
File "C:\Users\vetri\Google Drive\proj1\lib\site-packages\django\db\mig
rations\executor.py", line 147, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\vetri\Google Drive\proj1\lib\site-packages\django\db\mig
rations\migration.py", line 115, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, projec
t_state)
File "C:\Users\vetri\Google Drive\proj1\lib\site-packages\django\db\mig
rations\operations\models.py", line 59, in database_forwards
schema_editor.create_model(model)
File "C:\Users\vetri\Google Drive\proj1\lib\site-packages\django\db\bac
kends\base\schema.py", line 286, in create_model
self.execute(sql, params or None)
File "C:\Users\vetri\Google Drive\proj1\lib\site-packages\django\db\bac
kends\base\schema.py", line 111, in execute
cursor.execute(sql, params)
File "C:\Users\vetri\Google Drive\proj1\lib\site-packages\django\db\bac
kends\utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Users\vetri\Google Drive\proj1\lib\site-packages\django\db\bac
kends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Users\vetri\Google Drive\proj1\lib\site-packages\django\db\uti
ls.py", line 97, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Users\vetri\Google Drive\proj1\lib\site-packages\django\utils\
six.py", line 658, in reraise
raise value.with_traceback(tb)
File "C:\Users\vetri\Google Drive\proj1\lib\site-packages\django\db\bac
kends\utils.py", line 62, in execute
return self.cursor.execute(sql)
File "C:\Users\vetri\Google Drive\proj1\lib\site-packages\django\db\bac
kends\sqlite3\base.py", line 316, in execute
return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: table "Index_usermanagement" already exists
(proj1) C:\Users\vetri\Google Drive\proj1\src>
You must have run syncdb before doing the migrations or anothee possible reason is that using some other version of your database which already have this table
Try to remove your table 'Index_usermanagement' which already exists!
After that again run
python manage.py migrate
It looks like you may be moving to Django 1.8 for a model that already has an existing table. For Django 1.8, you'll want to use --fakeinitial option, which use to be implicit but now must be called explicitly starting in 1.8.
Documentation:
https://docs.djangoproject.com/en/1.8/ref/django-admin/#django-admin-option---fake-initial

Categories