Related
I successfully connected my Django app on my computer to Mlab using Djongo and the homepage on my localhost:8000 server seems to be displaying correctly. However, when I run 'py manage.py migrate', it gives a long list of errors in the terminal and I am not sure how to properly connect to the Mlabs database and populate it with data from the terminal of my Django app in my local computer. How do I migrate the database information onto my Mlab database? I'm not sure what the error is. I'm a little new to Django and MongoDB.
This is a part of the error I see:
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\djongo\cursor.py", line 51, in execute
self.result = Query(
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\djongo\sql2mongo\query.py", line 783, in __init__
self._query = self.parse()
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\djongo\sql2mongo\query.py", line 868, in parse
raise exe from e
djongo.exceptions.SQLDecodeError:
Keyword: None
Sub SQL: None
FAILED SQL: INSERT INTO "django_migrations" ("app", "name", "applied") VALUES (%(0)s, %(1)s, %(2)s)
Params: ['contenttypes', '0001_initial', datetime.datetime(2020, 7, 29, 21, 36, 46, 283726)]
Version: 1.3.3
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\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 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\base.py", line 369, in execute
output = self.handle(*args, **options)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\commands\migrate.py", line 231, in handle
post_migrate_state = executor.migrate(
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\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 "D:\Users\simon\AppData\Local\Programs\Python\Python38\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 "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\migrations\executor.py", line 246, in apply_migration
self.record_migration(migration)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\migrations\executor.py", line 261, in record_migration
self.recorder.record_applied(migration.app_label, migration.name)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\migrations\recorder.py", line 86, in record_applied
self.migration_qs.create(app=app, name=name)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\query.py", line 433, in create
obj.save(force_insert=True, using=self.db)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\base.py", line
745, in save
self.save_base(using=using, force_insert=force_insert,
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\base.py", line
782, in save_base
updated = self._save_table(
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\base.py", line
887, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\base.py", line
924, in _do_insert
return manager._insert(
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\query.py", line 1204, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\sql\compiler.py", line 1391, in execute_sql
PS D:\Business\Daydoa\v6-Django_with_MongoDB3\djongo_project> py manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial...This version of djongo does not support "schema validation using CONSTRAINT" fully. Visit https://www.patreon.com/nesdis
Traceback (most recent call last):
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\pymongo\mongo_client.py", line 1384, in _retry_with_session
return func(session, sock_info, retryable)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\pymongo\collection.py", line 2873, in _find_and_modify
out = self._command(sock_info, cmd,
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\pymongo\collection.py", line 235, in _command
return sock_info.command(
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\pymongo\pool.py", line 603, in command
return command(self.sock, dbname, spec, slave_ok,
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\pymongo\network.py", line 165, in command
helpers._check_command_response(
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\pymongo\helpers.py", line 159, in _check_command_response
raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: Transaction numbers are only allowed on storage engines that support document-level locking
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\djongo\sql2mongo\query.py", line 856, in parse
return handler(self, statement)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\djongo\sql2mongo\query.py", line 928, in _insert
query.execute()
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\djongo\sql2mongo\query.py", line 374, in execute
auto = self.db['__schema__'].find_one_and_update(
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\pymongo\collection.py", line 3134, in find_one_and_update
return self.__find_and_modify(filter, projection,
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\pymongo\collection.py", line 2884, in __find_and_modify
return self.__database.client._retryable_write(
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\pymongo\mongo_client.py", line 1491, in _retryable_write
return self._retry_with_session(retryable, func, s, None)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\pymongo\mongo_client.py", line 1425, in _retry_with_session
raise OperationFailure(errmsg, exc.code, exc.details)
pymongo.errors.OperationFailure: This MongoDB deployment does not support retryable writes. Please add retryWrites=false to your connection string.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\djongo\cursor.py", line 51, in execute
self.result = Query(
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\djongo\sql2mongo\query.py", line 783, in __init__
self._query = self.parse()
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\djongo\sql2mongo\query.py", line 868, in parse
raise exe from e
djongo.exceptions.SQLDecodeError:
Keyword: None
Sub SQL: None
FAILED SQL: INSERT INTO "django_migrations" ("app", "name", "applied") VALUES (%(0)s, %(1)s, %(2)s)
Params: ['contenttypes', '0001_initial', datetime.datetime(2020, 7, 29, 21, 37, 1, 197586)]
Version: 1.3.3
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\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 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\base.py", line 369, in execute
output = self.handle(*args, **options)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\commands\migrate.py", line 231, in handle
post_migrate_state = executor.migrate(
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\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 "D:\Users\simon\AppData\Local\Programs\Python\Python38\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 "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\migrations\executor.py", line 246, in apply_migration
self.record_migration(migration)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\migrations\executor.py", line 261, in record_migration
self.recorder.record_applied(migration.app_label, migration.name)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\migrations\recorder.py", line 86, in record_applied
self.migration_qs.create(app=app, name=name)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\query.py", line 433, in create
obj.save(force_insert=True, using=self.db)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\base.py", line
745, in save
self.save_base(using=using, force_insert=force_insert,
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\base.py", line
782, in save_base
updated = self._save_table(
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\base.py", line
887, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\base.py", line
924, in _do_insert
return manager._insert(
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\query.py", line 1204, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\sql\compiler.py", line 1391, in execute_sql
cursor.execute(sql, params)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\utils.py", line 100, in execute
return super().execute(sql, params)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\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 "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "D:\Users\simon\AppData\Local\Programs\Python\Python38\lib\site-packages\djongo\cursor.py", line 59, in execute
raise db_exe from e
django.db.utils.DatabaseError
I added a postgres JsonField in my model, and created data migration for it.
new_column = pg_fields.JSONField(default=dict, encoder=DjangoJSONEncoder)
Same logic is used for future data(in save() of model) and for historic data(data migration).
This is the format of data I am trying to save
data = {
'premium': obj.amount * obj.persons,
}
proposal.new_column = data
proposal.save()
Here, obj is the instance of some other model, and amount is the DecimalField of that model.
The logic runs fine in save() method of model class.
But on running data migration, I am getting this error
TypeError: Object of type Decimal is not JSON serializable
Stacktrace
Traceback (most recent call last):
File "./manage.py", line 21, in <module>
execute_from_command_line(sys.argv)
File "/home/simarpreet/Envs/j/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/simarpreet/Envs/j/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/simarpreet/Envs/j/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/simarpreet/Envs/j/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/home/simarpreet/Envs/j/lib/python3.7/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/home/simarpreet/Envs/j/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 234, in handle
fake_initial=fake_initial,
File "/home/simarpreet/Envs/j/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/simarpreet/Envs/j/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/simarpreet/Envs/j/lib/python3.7/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/simarpreet/Envs/j/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/simarpreet/Envs/j/lib/python3.7/site-packages/django/db/migrations/operations/special.py", line 190, in database_forwards
self.code(from_state.apps, schema_editor)
File "/home/simarpreet/Aegon/jarvis/insurance/travel/proposal/migrations/0006_auto_20191111_1943.py", line 34, in repopulate_slab_in_proposals
proposal.save()
File "/home/simarpreet/Envs/j/lib/python3.7/site-packages/django/db/models/base.py", line 741, in save
force_update=force_update, update_fields=update_fields)
File "/home/simarpreet/Envs/j/lib/python3.7/site-packages/django/db/models/base.py", line 779, in save_base
force_update, using, update_fields,
File "/home/simarpreet/Envs/j/lib/python3.7/site-packages/django/db/models/base.py", line 851, in _save_table
forced_update)
File "/home/simarpreet/Envs/j/lib/python3.7/site-packages/django/db/models/base.py", line 900, in _do_update
return filtered._update(values) > 0
File "/home/simarpreet/Envs/j/lib/python3.7/site-packages/django/db/models/query.py", line 760, in _update
return query.get_compiler(self.db).execute_sql(CURSOR)
File "/home/simarpreet/Envs/j/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1429, in execute_sql
cursor = super().execute_sql(result_type)
File "/home/simarpreet/Envs/j/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1100, in execute_sql
cursor.execute(sql, params)
File "/home/simarpreet/Envs/j/lib/python3.7/site-packages/django/db/backends/utils.py", line 99, in execute
return super().execute(sql, params)
File "/home/simarpreet/Envs/j/lib/python3.7/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/simarpreet/Envs/j/lib/python3.7/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/simarpreet/Envs/j/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/simarpreet/Envs/j/lib/python3.7/site-packages/psycopg2/_json.py", line 78, in getquoted
s = self.dumps(self.adapted)
File "/home/simarpreet/Envs/j/lib/python3.7/site-packages/django/contrib/postgres/fields/jsonb.py", line 27, in dumps
return json.dumps(obj, **options)
File "/usr/lib/python3.7/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python3.7/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.7/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python3.7/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Decimal is not JSON serializable
But as per the documentation of Django data migration (https://docs.djangoproject.com/en/2.2/topics/migrations/) , Django can serialze the decimal.Decimal value. Following is the screenshot for reference.
Can someone suggest why am I getting this error?
I was getting this issue because I forgot to run migration after adding the encoder
new_column = pg_fields.JSONField(default=dict, encoder=DjangoJSONEncoder)
Running migration again python manage.py migrate solved the problem.
Posting the solution in case someone else does such things like me.
While attempting to migrate database changes using django on Heroku, I get:
psycopg2.ProgrammingError: permission denied for relation django_migrations
Other people have been able to resolve this issue by granting the appropriate privileges (e.g., Permission denied for relation). Unfortunately, I can't grant permissions or create a new user on the Heroku hobby tier.
I was able to roll back a few migrations, but eventually I got:
django.db.utils.ProgrammingError: table "labs_branch_tests" does not exist
and could not migrate back to newer migrations.
Full traceback for permission denied for relation django_migrations
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, 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 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/app/.heroku/python/lib/python3.6/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 "/app/.heroku/python/lib/python3.6/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 "/app/.heroku/python/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 "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 73, in record_applied
self.migration_qs.create(app=app, name=name)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 394, in create
obj.save(force_insert=True, using=self.db)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py", line 807, in save
force_update=force_update, update_fields=update_fields)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py", line 837, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py", line 923, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py", line 962, in _do_insert
using=using, raw=raw)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 1076, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1107, in execute_sql
cursor.execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: permission denied for relation django_migrations
Full traceback for psycopg2.ProgrammingError: table "labs_branch_tests" does not exist
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, 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 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 119, in migrate
state = self._migrate_all_backwards(plan, full_plan, fake=fake)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 194, in _migrate_all_backwards
self.unapply_migration(states[migration], migration, fake=fake)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 264, in unapply_migration
state = migration.unapply(state, schema_editor)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/migration.py", line 178, in unapply
operation.database_backwards(self.app_label, schema_editor, from_state, to_state)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 95, in database_backwards
schema_editor.remove_field(from_model, from_model._meta.get_field(self.name))
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 457, in remove_field
return self.delete_model(field.remote_field.through)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 324, in delete_model
"table": self.quote_name(model._meta.db_table),
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 120, in execute
cursor.execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: table "labs_branch_tests" does not exist
I was able to get around this by granting privileges to the user in the postgres terminal.
First connect to postgres through the Heroku CLI (heroku pg:psql).
This is clunky but get your Heroku postgres username by calling \c in the postgres terminal. (The last line of output will be You are now connected to database DATABASE_NAME as user USERNAME.)
Then grant privileges as others have shown:
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public to USERNAME;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public to USERNAME;
GRANT ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA public to USERNAME;
In my case this was because my free database reached its row limit and write access was revoked:
$ heroku pg:info
=== DATABASE_URL
Plan: Hobby-dev
Status: Available
Connections: 0/20
PG Version: 10.4
[..]
Rows: 11683/10000 (Write access revoked)
[..]
for me yes you can tell the write access was revoked with
$ heroku pg:info
...
Rows: 11683/10000 (Write access revoked)
but I didn't want to dump the whole database. Instead I identify the biggest tables with
$ heroku pg:psql
> select
table_name
, pg_relation_size(quote_ident(table_name)) as "raw_size"
, pg_size_pretty(pg_relation_size(quote_ident(table_name)) as "size"
from information_schema.tables
where table_schema = 'public'
order by "raw_size" desc;
and then I clear the biggest tables with
> delete from <table name>;
I have spent hours trying to get a vagrant django development environment running on my Windows 7 machine.
So far I have:
Used vagrant to create an ubuntu vm
Installed python 3.4.3
Installed mariadb 5.5
Installed django 1.9
Installed mysql-connector-python 2.1.3
Created a new DB in mysql
On a fresh django install, when I try to run python manage.py migrate, I get the following error:
Operations to perform:
Apply all migrations: sessions, auth, contenttypes, admin
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial...Traceback (most recent call last):
File "/home/vagrant/djangoenv/lib/python3.4/site-packages/mysql/connector/django/base.py", line 177, in _execute_wrapper
return method(query, args)
File "/home/vagrant/djangoenv/lib/python3.4/site-packages/mysql/connector/cursor.py", line 515, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "/home/vagrant/djangoenv/lib/python3.4/site-packages/mysql/connector/cursor.py", line 434, in _handle_result
self._handle_noresultset(result)
File "/home/vagrant/djangoenv/lib/python3.4/site-packages/mysql/connector/cursor.py", line 404, in _handle_noresultset
self._warnings[0][1], self._warnings[0][2])
mysql.connector.errors.DatabaseError: 1265: Data truncated for column 'applied' at row 1
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/vagrant/djangoenv/lib/python3.4/site-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
utility.execute()
File "/home/vagrant/djangoenv/lib/python3.4/site-packages/django/core/management/__init__.py", line 342, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/vagrant/djangoenv/lib/python3.4/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/vagrant/djangoenv/lib/python3.4/site-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/home/vagrant/djangoenv/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 200, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/home/vagrant/djangoenv/lib/python3.4/site-packages/django/db/migrations/executor.py", line 92, in migrate
self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/home/vagrant/djangoenv/lib/python3.4/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/home/vagrant/djangoenv/lib/python3.4/site-packages/django/db/migrations/executor.py", line 204, in apply_migration
self.recorder.record_applied(migration.app_label, migration.name)
File "/home/vagrant/djangoenv/lib/python3.4/site-packages/django/db/migrations/recorder.py", line 73, in record_applied
self.migration_qs.create(app=app, name=name)
File "/home/vagrant/djangoenv/lib/python3.4/site-packages/django/db/models/query.py", line 401, in create
obj.save(force_insert=True, using=self.db)
File "/home/vagrant/djangoenv/lib/python3.4/site-packages/django/db/models/base.py", line 700, in save
force_update=force_update, update_fields=update_fields)
File "/home/vagrant/djangoenv/lib/python3.4/site-packages/django/db/models/base.py", line 728, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/vagrant/djangoenv/lib/python3.4/site-packages/django/db/models/base.py", line 812, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/home/vagrant/djangoenv/lib/python3.4/site-packages/django/db/models/base.py", line 851, in _do_insert
using=using, raw=raw)
File "/home/vagrant/djangoenv/lib/python3.4/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/vagrant/djangoenv/lib/python3.4/site-packages/django/db/models/query.py", line 1039, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/home/vagrant/djangoenv/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 1064, in execute_sql
cursor.execute(sql, params)
File "/home/vagrant/djangoenv/lib/python3.4/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/vagrant/djangoenv/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/vagrant/djangoenv/lib/python3.4/site-packages/mysql/connector/django/base.py", line 227, in execute
return self._execute_wrapper(self.cursor.execute, query, new_args)
File "/home/vagrant/djangoenv/lib/python3.4/site-packages/mysql/connector/django/base.py", line 195, in _execute_wrapper
utils.DatabaseError(err.msg), sys.exc_info()[2])
File "/home/vagrant/djangoenv/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/home/vagrant/djangoenv/lib/python3.4/site-packages/mysql/connector/django/base.py", line 177, in _execute_wrapper
return method(query, args)
File "/home/vagrant/djangoenv/lib/python3.4/site-packages/mysql/connector/cursor.py", line 515, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "/home/vagrant/djangoenv/lib/python3.4/site-packages/mysql/connector/cursor.py", line 434, in _handle_result
self._handle_noresultset(result)
File "/home/vagrant/djangoenv/lib/python3.4/site-packages/mysql/connector/cursor.py", line 404, in _handle_noresultset
self._warnings[0][1], self._warnings[0][2])
django.db.utils.DatabaseError: Data truncated for column 'applied' at row 1
I couldn't find anything that had that exact error. Anyone have any ideas?
It turns out the issue isn't how the table or column is defined, so just looking at the table or column definitions isn't going to do you any good*.
In your [project_name]/settings.py file make sure that under ...
DATABASES = { ...
... the ENGINE attribute is set to django.db.backends.mysql as opposed to mysql.connector.django.
*Although, you should make sure that the column type for applied is DATETIME(6), which shouldn't be a problem since that's the default value Django gives it, so unless you somehow changed it, you should be fine.
The table is django_migrations in case you are wanting to look up the data type.
If you store 12345678901234567890 into an INT, it will be truncated.
If you store 'asdfasdfasdf' into VARCHAR(5), it will be truncated.
Look at your table definition and your data. You can figure this out.
So I changed some models and the data structure changed dramatically and made a migration fail. I cleared out the tables that were affected in the database, deleted all the migration files and then tried to migrate again.
It made migrations for all the tables (even the ones that I left behind in the database) and when I try to migrate I get this error:
Operations to perform:
Apply all migrations: study
Running migrations:
Rendering model states... DONE
Applying study.0001_initial...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 393, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 444, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 222, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 110, in migrate
self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 148, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py", line 115, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/models.py", line 59, in database_forwards
schema_editor.create_model(model)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 286, in create_model
self.execute(sql, params or None)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 111, in execute
cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 97, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 62, in execute
return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "study_language" already exists
If I go into the migration files and delete the spots where it is trying to migrate an existing table, I get this error...
Operations to perform:
Apply all migrations: study
Running migrations:
Rendering model states... DONE
Applying study.0001_initial...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 393, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 444, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 222, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 110, in migrate
self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 148, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py", line 115, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/models.py", line 59, in database_forwards
schema_editor.create_model(model)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 236, in create_model
definition, extra_params = self.column_sql(model, field)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 135, in column_sql
db_params = field.db_parameters(connection=self.connection)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 2003, in db_parameters
return {"type": self.db_type(connection), "check": []}
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 1994, in db_type
rel_field = self.related_field
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 1897, in related_field
return self.foreign_related_fields[0]
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 1631, in foreign_related_fields
return tuple(rhs_field for lhs_field, rhs_field in self.related_fields)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 1618, in related_fields
self._related_fields = self.resolve_related_fields()
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 1603, in resolve_related_fields
raise ValueError('Related model %r cannot be resolved' % self.rel.to)
ValueError: Related model u'study.Language' cannot be resolved
How can I get this to work without deleting all the tables I left behind in the db?
What worked for me is that I commented out all of the migrations that were not in the db in models.py. I then cleaned the migration folder again and ran the command...
$ python manage.py makemigrations
$ python manage.py migrate appname --fake-initial
Which went smoothly and then I uncommented the models I wanted to add to the db and ran a normal migration...
$ python manage.py makemigrations
$ python manage.py migrate
Everything went smoothly