Hawthorn to Ironwood Migration (Open edX) - python

I am trying to execute database migration from hawthorn.master to ironwood.master, during MySQL migration, getting the error like this:
Applying content_type_gating.0002_auto_20181119_0959…Traceback (most recent call last):
File “./manage.py”, line 123, in
execute_from_command_line([sys.argv[0]] + django_args)
File “/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/core/management/init.py”, line 364, in execute_from_command_line
utility.execute()
File “/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/core/management/init.py”, line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File “/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/core/management/base.py”, line 283, in run_from_argv
self.execute(*args, **cmd_options)
File “/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/core/management/base.py”, line 330, in execute
output = self.handle(*args, **options)
File “/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py”, line 204, in handle
fake_initial=fake_initial,
File “/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/db/migrations/executor.py”, line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File “/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/db/migrations/executor.py”, line 145, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File “/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/db/migrations/executor.py”, line 250, in apply_migration
self.recorder.record_applied(migration.app_label, migration.name)
File “/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/db/migrations/recorder.py”, line 73, in record_applied
self.migration_qs.create(app=app, name=name)
File “/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/db/models/query.py”, line 394, in create
obj.save(force_insert=True, using=self.db)
File “/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/db/models/base.py”, line 808, in save
force_update=force_update, update_fields=update_fields)
File “/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/db/models/base.py”, line 838, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File “/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/db/models/base.py”, line 924, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File “/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/db/models/base.py”, line 963, in _do_insert
using=using, raw=raw)
File “/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/db/models/manager.py”, line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File “/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/db/models/query.py”, line 1079, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File “/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py”, line 1112, in execute_sql
cursor.execute(sql, params)
File “/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/db/backends/utils.py”, line 64, in execute
return self.cursor.execute(sql, params)
File “/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/db/utils.py”, line 94, in exit
six.reraise(dj_exc_type, dj_exc_value, traceback)
File “/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/db/backends/utils.py”, line 64, in execute
return self.cursor.execute(sql, params)
File “/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py”, line 101, in execute
return self.cursor.execute(query, args)
File “/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/MySQLdb/cursors.py”, line 205, in execute
self.errorhandler(self, exc, value)
File “/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/MySQLdb/connections.py”, line 36, in defaulterrorhandler
raise errorclass, errorvalue
django.db.utils.IntegrityError: (1062, “Duplicate entry ‘0’ for key ‘PRIMARY’”)
Here is the application link: https://github.com/edx/edx-platform/tree/master/openedx/features/content_type_gating
In addition, this table has no data.
Can anyone help to solve this?

Related

Programming Error: With PhoneNumberField in Django

After pip install phone number field on global as well as in virtual env
and importing in the models.py
I am facing programming error.
Details are as follows
DB is POSTGRESQL
models.py
from django.db import models
from phonenumber_field.modelfields import PhoneNumberField
class Agent(models.Model):
agency_name = models.CharField(max_length=200)
prop_name = models.CharField(max_length=30)
agency_address = models.CharField(max_length=300)
agency_city = models.CharField(max_length=50)
agency_country = models.CharField(max_length=50)
email_address = models.EmailField(max_length=50)
contact_nu = PhoneNumberField()
def __str__(self):
return self.agency_name
admin.py
#admin.register(Agent)
class AgentAdmin(admin.ModelAdmin):
list_display = ('agency_name', 'prop_name', 'agency_address', 'agency_city', 'agency_country', 'email_address', 'contact_nu')
Error:
[07/Jun/2021 09:52:34] "GET /admin/ HTTP/1.1" 200 5546
Internal Server Error: /admin/rk003/agent/
Traceback (most recent call last):
File "/Users/rishipalsingh/Projects/notes/mdndjango/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedColumn: column rk003_agent.contact_nu does not exist
Error at the time of migrate is as follows
During Migration there is no error. Makemigration works fine and create models. The check also dont return any error. Just at the time of migrate below error pops up
File "/Users/rishipalsingh/Projects/notes/mdndjango/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedColumn: column "contact_nu" of relation "rk003_agent" does not exist
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/rishipalsingh/Projects/notes/mdndjango/rk002/manage.py", line 22, in <module>
main()
File "/Users/rishipalsingh/Projects/notes/mdndjango/rk002/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/Users/rishipalsingh/Projects/notes/mdndjango/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "/Users/rishipalsingh/Projects/notes/mdndjango/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/rishipalsingh/Projects/notes/mdndjango/venv/lib/python3.9/site-packages/django/core/management/base.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/rishipalsingh/Projects/notes/mdndjango/venv/lib/python3.9/site-packages/django/core/management/base.py", line 398, in execute
output = self.handle(*args, **options)
File "/Users/rishipalsingh/Projects/notes/mdndjango/venv/lib/python3.9/site-packages/django/core/management/base.py", line 89, in wrapped
res = handle_func(*args, **kwargs)
File "/Users/rishipalsingh/Projects/notes/mdndjango/venv/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 244, in handle
post_migrate_state = executor.migrate(
File "/Users/rishipalsingh/Projects/notes/mdndjango/venv/lib/python3.9/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 "/Users/rishipalsingh/Projects/notes/mdndjango/venv/lib/python3.9/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 "/Users/rishipalsingh/Projects/notes/mdndjango/venv/lib/python3.9/site-packages/django/db/migrations/executor.py", line 227, in apply_migration
state = migration.apply(state, schema_editor)
File "/Users/rishipalsingh/Projects/notes/mdndjango/venv/lib/python3.9/site-packages/django/db/migrations/migration.py", line 126, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/Users/rishipalsingh/Projects/notes/mdndjango/venv/lib/python3.9/site-packages/django/db/migrations/operations/fields.py", line 244, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/Users/rishipalsingh/Projects/notes/mdndjango/venv/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 594, in alter_field
self._alter_field(model, old_field, new_field, old_type, new_type,
File "/Users/rishipalsingh/Projects/notes/mdndjango/venv/lib/python3.9/site-packages/django/db/backends/postgresql/schema.py", line 196, in _alter_field
super()._alter_field(
File "/Users/rishipalsingh/Projects/notes/mdndjango/venv/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 751, in _alter_field
self.execute(
File "/Users/rishipalsingh/Projects/notes/mdndjango/venv/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 145, in execute
cursor.execute(sql, params)
File "/Users/rishipalsingh/Projects/notes/mdndjango/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 98, in execute
return super().execute(sql, params)
File "/Users/rishipalsingh/Projects/notes/mdndjango/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/Users/rishipalsingh/Projects/notes/mdndjango/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/Users/rishipalsingh/Projects/notes/mdndjango/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/Users/rishipalsingh/Projects/notes/mdndjango/venv/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Users/rishipalsingh/Projects/notes/mdndjango/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column "contact_nu" of relation "rk003_agent" does not exist
Thanks
Use a CharField, easy to handle yourself and won't cause any excessive errors.

django.db.utils.DatabaseError with Django and Djongo

I keep running into the django.db.utils.DatabaseError error with trying to migrate the database changes in a Django project.
Terminal Output:
Running migrations:
Not implemented alter command for SQL ALTER TABLE "project_name_user" ALTER COLUMN "age" TYPE double
Applying project_name.0002_auto_20210415_1452...Traceback (most recent call last):
File "...\.venv\lib\site-packages\djongo\cursor.py", line 51, in execute
self.result = Query(
File "...\.venv\lib\site-packages\djongo\sql2mongo\query.py", line 783, in __init__
self._query = self.parse()
File "...\.venv\lib\site-packages\djongo\sql2mongo\query.py", line 875, in parse
raise e
File "...\.venv\lib\site-packages\djongo\sql2mongo\query.py", line 856, in parse
return handler(self, statement)
File "...\.venv\lib\site-packages\djongo\sql2mongo\query.py", line 888, in _alter
query = AlterQuery(self.db, self.connection_properties, sm, self._params)
File "...\.venv\lib\site-packages\djongo\sql2mongo\query.py", line 425, in __init__
super().__init__(*args)
File "...\.venv\lib\site-packages\djongo\sql2mongo\query.py", line 84, in __init__
super().__init__(*args)
File "...\.venv\lib\site-packages\djongo\sql2mongo\query.py", line 62, in __init__
self.parse()
File "...\.venv\lib\site-packages\djongo\sql2mongo\query.py", line 441, in parse
self._alter(statement)
File "...\.venv\lib\site-packages\djongo\sql2mongo\query.py", line 500, in _alter
raise SQLDecodeError(f'Unknown token: {tok}')
djongo.exceptions.SQLDecodeError:
Keyword: Unknown token: TYPE
Sub SQL: None
FAILED SQL: ('ALTER TABLE "project_name_user" ALTER COLUMN "age" TYPE double',)
Params: ([],)
Version: 1.3.4
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "...\.venv\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "...\.venv\lib\site-packages\djongo\cursor.py", line 59, in execute
raise db_exe from e
djongo.database.DatabaseError
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "...\manage.py", line 22, in <module>
main()
File "...\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "...\.venv\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "...\.venv\lib\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "...\.venv\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "...\.venv\lib\site-packages\django\core\management\base.py", line 369, in execute
output = self.handle(*args, **options)
File "...\.venv\lib\site-packages\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "...\.venv\lib\site-packages\django\core\management\commands\migrate.py", line 231, in handle
post_migrate_state = executor.migrate(
File "...\.venv\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 "...\.venv\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 "...\.venv\lib\site-packages\django\db\migrations\executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "...\.venv\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 "...\.venv\lib\site-packages\django\db\migrations\operations\fields.py", line 249, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "...\.venv\lib\site-packages\django\db\backends\base\schema.py", line 564, in alter_field
self._alter_field(model, old_field, new_field, old_type, new_type,
File "...\.venv\lib\site-packages\django\db\backends\base\schema.py", line 710, in _alter_field
self.execute(
File "...\.venv\lib\site-packages\django\db\backends\base\schema.py", line 142, in execute
cursor.execute(sql, params)
File "...\.venv\lib\site-packages\django\db\backends\utils.py", line 100, in execute
return super().execute(sql, params)
File "...\.venv\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 "...\.venv\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "...\.venv\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "...\.venv\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "...\.venv\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "...\.venv\lib\site-packages\djongo\cursor.py", line 59, in execute
raise db_exe from e
django.db.utils.DatabaseError
I am stumped on how to solve this issue. I have tried sqlparse==0.2.4 as suggested here but it did not work for me.
Deleted everything in the migrations folder and started from scratch. The same error pops up but now it is even longer.
I understand there is a not implemented error but not sure how to fix it.
The models.py:
from djongo import models
class User(models.Model):
age = models.FloatField()
height = models.FloatField()
waist = models.FloatField()
weight = models.FloatField()

Django django.db.utils.IntegrityError: NOT NULL constraint failed: new__users_profile.location

I have the following model
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="profile")
image = models.ImageField(default='default.jpg', upload_to='profile_pics')
location = models.CharField(default=None, max_length=5)
def __str__(self):
return f'{self.user.username} Profile'
I was testing some things out and put null=True for location. Then I changed it to default=None. After that when I tried to run makemigrations followed by migrate.
I got the following error
Full Traceback
Traceback (most recent call last):
File "C:\Users\uddin\project\trade\manage.py", line 22, in <module>
main()
File "C:\Users\uddin\project\trade\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\uddin\Envs\web\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\uddin\Envs\web\lib\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\uddin\Envs\web\lib\site-packages\django\core\management\base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\uddin\Envs\web\lib\site-packages\django\core\management\base.py", line 371, in execute
output = self.handle(*args, **options)
File "C:\Users\uddin\Envs\web\lib\site-packages\django\core\management\base.py", line 85, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\uddin\Envs\web\lib\site-packages\django\core\management\commands\migrate.py", line 243, in handle
post_migrate_state = executor.migrate(
File "C:\Users\uddin\Envs\web\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\uddin\Envs\web\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\uddin\Envs\web\lib\site-packages\django\db\migrations\executor.py", line 227, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\uddin\Envs\web\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\uddin\Envs\web\lib\site-packages\django\db\migrations\operations\fields.py", line 236, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "C:\Users\uddin\Envs\web\lib\site-packages\django\db\backends\sqlite3\schema.py", line 138, in alter_field
super().alter_field(model, old_field, new_field, strict=strict)
File "C:\Users\uddin\Envs\web\lib\site-packages\django\db\backends\base\schema.py", line 571, in alter_field
self._alter_field(model, old_field, new_field, old_type, new_type,
File "C:\Users\uddin\Envs\web\lib\site-packages\django\db\backends\sqlite3\schema.py", line 360, in _alter_field
self._remake_table(model, alter_field=(old_field, new_field))
File "C:\Users\uddin\Envs\web\lib\site-packages\django\db\backends\sqlite3\schema.py", line 283, in _remake_table
self.execute("INSERT INTO %s (%s) SELECT %s FROM %s" % (
File "C:\Users\uddin\Envs\web\lib\site-packages\django\db\backends\base\schema.py", line 142, in execute
cursor.execute(sql, params)
File "C:\Users\uddin\Envs\web\lib\site-packages\django\db\backends\utils.py", line 98, in execute
return super().execute(sql, params)
File "C:\Users\uddin\Envs\web\lib\site-packages\django\db\backends\utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\uddin\Envs\web\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\uddin\Envs\web\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\uddin\Envs\web\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\uddin\Envs\web\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\uddin\Envs\web\lib\site-packages\django\db\backends\sqlite3\base.py", line 413, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: new__users_profile.location
I went through the migrations folder and changed each migration that had null=True to default=None, but I still seem to get the error? Anything I'm not seeing?

(migrations table) django.db.utils.IntegrityError: null value in column "id" violates not-null constraint

When I run migrate it raises this error. I can't figure out where can be the problem because it is caused by the built-in functionality of adding migration data inside the migrations table.
Running migrations:
Applying matches.0023_auto_20191102_1717...Traceback (most recent call last):
File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
psycopg2.IntegrityError: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, matches, 0023_auto_20191102_1717, 2019-11-02 17:17:38.046421+01).
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 "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute
output = self.handle(*args, **options)
File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 200, in handle
fake_initial=fake_initial,
File "/home/milano/.virtualenvs/sport_database/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 "/home/milano/.virtualenvs/sport_database/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 "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/migrations/executor.py", line 250, in apply_migration
self.recorder.record_applied(migration.app_label, migration.name)
File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 71, in record_applied
self.migration_qs.create(app=app, name=name)
File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/models/query.py", line 417, in create
obj.save(force_insert=True, using=self.db)
File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/models/base.py", line 729, in save
force_update=force_update, update_fields=update_fields)
File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/models/base.py", line 759, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/models/base.py", line 842, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/models/base.py", line 880, in _do_insert
using=using, raw=raw)
File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/models/query.py", line 1125, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1285, in execute_sql
cursor.execute(sql, params)
File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/milano/.virtualenvs/sport_database/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, matches, 0023_auto_20191102_1717, 2019-11-02 17:17:38.046421+01).
Do you know what to do? I can't just drop and recreate the DB.

Cannot create django test database

I have a projects with hundreds of migrations. When I try to run the tests it gives me the stacktrace:
./manage.py test
Creating test database for alias 'default'...
Traceback (most recent call last):
File "/project/lib/python3.5/site-packages/django/db/backends/utils.py", line 62, in execute
return self.cursor.execute(sql)
File "/project/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 326, in execute
return Database.Cursor.execute(self, query)
sqlite3.OperationalError: near "USING": syntax error
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 "/project/lib/python3.5/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/project/lib/python3.5/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/project/lib/python3.5/site-packages/django/core/management/commands/test.py", line 29, in run_from_argv
super(Command, self).run_from_argv(argv)
File "/project/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/project/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/project/lib/python3.5/site-packages/django/core/management/commands/test.py", line 62, in handle
failures = test_runner.run_tests(test_labels)
File "/project/lib/python3.5/site-packages/django/test/runner.py", line 601, in run_tests
old_config = self.setup_databases()
File "/project/lib/python3.5/site-packages/django/test/runner.py", line 546, in setup_databases
self.parallel, **kwargs
File "/project/lib/python3.5/site-packages/django/test/utils.py", line 187, in setup_databases
serialize=connection.settings_dict.get('TEST', {}).get('SERIALIZE', True),
File "/project/lib/python3.5/site-packages/django/db/backends/base/creation.py", line 69, in create_test_db
run_syncdb=True,
File "/project/lib/python3.5/site-packages/django/core/management/__init__.py", line 131, in call_command
return command.execute(*args, **defaults)
File "/project/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/project/lib/python3.5/site-packages/wagtail_modeltranslation/management/commands/migrate_translation.py", line 25, in handle
super(Command, self).handle(*args, **options)
File "/project/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/project/lib/python3.5/site-packages/django/db/migrations/executor.py", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/project/lib/python3.5/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/project/lib/python3.5/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/project/lib/python3.5/site-packages/django/db/migrations/migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/project/lib/python3.5/site-packages/django/db/migrations/operations/special.py", line 108, in database_forwards
self._run_sql(schema_editor, self.sql)
File "/project/lib/python3.5/site-packages/django/db/migrations/operations/special.py", line 133, in _run_sql
schema_editor.execute(statement, params=None)
File "/project/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 136, in execute
cursor.execute(sql, params)
File "/project/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/project/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/project/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/project/lib/python3.5/site-packages/django/db/backends/utils.py", line 62, in execute
return self.cursor.execute(sql)
File "/project/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 326, in execute
return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: near "USING": syntax error
From this alone I cannot seem to figure out the cause (which probably is a migration somewhere).
When I try to run the same command using a postgres database, I get a error
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/project/lib/python3.5/dist-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/project/lib/python3.5/dist-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/project/lib/python3.5/dist-packages/django/core/management/commands/test.py", line 29, in run_from_argv
super(Command, self).run_from_argv(argv)
File "/project/lib/python3.5/dist-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/project/lib/python3.5/dist-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/project/lib/python3.5/dist-packages/django/core/management/commands/test.py", line 62, in handle
failures = test_runner.run_tests(test_labels)
File "/project/lib/python3.5/dist-packages/django/test/runner.py", line 601, in run_tests
old_config = self.setup_databases()
File "/project/lib/python3.5/dist-packages/django/test/runner.py", line 546, in setup_databases
self.parallel, **kwargs
File "/project/lib/python3.5/dist-packages/django/test/utils.py", line 187, in setup_databases
serialize=connection.settings_dict.get('TEST', {}).get('SERIALIZE', True),
File "/project/lib/python3.5/dist-packages/django/db/backends/base/creation.py", line 69, in create_test_db
run_syncdb=True,
File "/project/lib/python3.5/dist-packages/django/core/management/__init__.py", line 131, in call_command
return command.execute(*args, **defaults)
File "/project/lib/python3.5/dist-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/project/lib/python3.5/dist-packages/wagtail_modeltranslation/management/commands/migrate_translation.py", line 25, in handle
super(Command, self).handle(*args, **options)
File "/project/lib/python3.5/dist-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/project/lib/python3.5/dist-packages/django/db/migrations/executor.py", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/project/lib/python3.5/dist-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/project/lib/python3.5/dist-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/project/lib/python3.5/dist-packages/django/db/migrations/migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/project/lib/python3.5/dist-packages/django/db/migrations/operations/fields.py", line 221, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/project/lib/python3.5/dist-packages/django/db/backends/base/schema.py", line 531, in alter_field
old_db_params, new_db_params, strict)
File "/project/lib/python3.5/dist-packages/django/db/backends/postgresql/schema.py", line 112, in _alter_field
new_db_params, strict,
File "/project/lib/python3.5/dist-packages/django/db/backends/base/schema.py", line 635, in _alter_field
new_default = self.effective_default(new_field)
File "/project/lib/python3.5/dist-packages/django/db/backends/base/schema.py", line 245, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "/project/lib/python3.5/dist-packages/django/db/models/fields/__init__.py", line 770, in get_db_prep_save
prepared=False)
File "/project/lib/python3.5/dist-packages/django/db/models/fields/__init__.py", line 762, in get_db_prep_value
value = self.get_prep_value(value)
File "/project/lib/python3.5/dist-packages/django/db/models/fields/__init__.py", line 1043, in get_prep_value
return self.to_python(value)
File "/project/lib/python3.5/dist-packages/django/db/models/fields/__init__.py", line 1036, in to_python
params={'value': value},
django.core.exceptions.ValidationError: ["'N' value must be either True or False."]
Since I use the value N all over the project, this does not help much either.
How do I go about finding the migration that causes these errors?
You can add the --verbosity of -v option when running your tests, which adds more verbosity to the output of your tests:
python manage.py test -v 2
or
python manage.py test --verbosity=2
Level 2 should show you all the migrations. Level 3 even shows more details, but I don't think it'll add much in your case.
This will show you which migration is causing the issue.

Categories