Related
One of my python modules(which is not directly associated with Django application, same project though) is supposed to use Django models(that belong to original Django application) for convenient DB queries via Django-ORM.
However, this only works well until there is a DB or site-to-site connection issue.
Reproduced 100% times with following steps:
Create models.
Import models by setting up Django in any python module
os.environ['DJANGO_SETTINGS_MODULE'] = 'ServiceName.settings'
django.setup()
from ServiceName.models import ModelName
Use models via django ORM to fetch some data from DB on service startup(lets call it step 1) and later on by some triggers(lets call it step 2). In my case data is fetched once on startup, and all the subsequent times on certain Kafka event.
e.g.
query_results = ModelName.objects.filter(your_filter=some_value)
4.Shutdown PostgreSQL instance after step one, but before step 2
Not sure if related to the issue, but we are using pg-bouncer as a connection point.
5.Start PostgreSQL, trigger step2.
First attemp to fetch the data fails with the following trace:
Traceback (most recent call last):
File "/opt/services/_init_.py", line 113, in observe_events
if callback(event):
File "services_logparser_core.py", line 193, in process_event
r_handler.update_rules()
File "services_logparser_core.py", line 48, in update_rules
self.rules_list = self._fetch_enabled_rules()
File "services_logparser_core.py", line 69, in _fetch_enabled_rules
for sr in enabled_search_rules:
File "/usr/local/lib/python3.8/dist-packages/django/db/models/query.py", line 280, in _iter_
self._fetch_all()
File "/usr/local/lib/python3.8/dist-packages/django/db/models/query.py", line 1324, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/usr/local/lib/python3.8/dist-packages/django/db/models/query.py", line 51, in _iter_
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/usr/local/lib/python3.8/dist-packages/django/db/models/sql/compiler.py", line 1175, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python3.8/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.8/dist-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/usr/local/lib/python3.8/dist-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.8/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.8/dist-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
django.db.utils.OperationalError: SSL SYSCALL error: EOF detected
All the subsequent attempts to fetch the data fail with:
Traceback (most recent call last):
File "/opt/services/__init__.py", line 113, in observe_events
if callback(event):
File "services_logparser_core.py", line 193, in process_event
r_handler.update_rules()
File "services_logparser_core.py", line 48, in update_rules
self.rules_list = self._fetch_enabled_rules()
File "services_logparser_core.py", line 69, in _fetch_enabled_rules
for sr in enabled_search_rules:
File "/usr/local/lib/python3.8/dist-packages/django/db/models/query.py", line 280, in __iter__
self._fetch_all()
File "/usr/local/lib/python3.8/dist-packages/django/db/models/query.py", line 1324, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/usr/local/lib/python3.8/dist-packages/django/db/models/query.py", line 51, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/usr/local/lib/python3.8/dist-packages/django/db/models/sql/compiler.py", line 1173, in execute_sql
cursor = self.connection.cursor()
File "/usr/local/lib/python3.8/dist-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/django/db/backends/base/base.py", line 259, in cursor
return self._cursor()
File "/usr/local/lib/python3.8/dist-packages/django/db/backends/base/base.py", line 237, in _cursor
return self._prepare_cursor(self.create_cursor(name))
File "/usr/local/lib/python3.8/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.8/dist-packages/django/db/backends/base/base.py", line 237, in _cursor
return self._prepare_cursor(self.create_cursor(name))
File "/usr/local/lib/python3.8/dist-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/django/db/backends/postgresql/base.py", line 236, in create_cursor
cursor = self.connection.cursor()
django.db.utils.InterfaceError: connection already closed
Application restart fixes the problem.
I don't set up a connection to the db manually anywhere(I only setup DB credentials), all the connections are handled by Django ORM.
Is there any django/psycopg setting can be configured inside the project so that new connection is established each time db query is required?
Django==3.2.6
from django import db
And
db.close_old_connections()
before
query_results = ModelName.objects.filter(your_filter=some_value)
Did the trick.
close_old_connections() method does not just close old connections, but also check if current connection is still usable. In case connection returns an error it will be closed. New connection opens automatically.
The program was quite running well a few minutes ago, after adding SlugField to my post URLs which I feel like it has nothing to do with the error, this is what I get when I run Python3 manage.py runserver in my terminal.
System check identified some issues:
WARNINGS:
base.Post.tags: (fields.W340) null has no effect on ManyToManyField.
System check identified 1 issue (0 silenced).
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/home/peace/.local/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql)
File "/home/peace/.local/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 394, in execute
return Database.Cursor.execute(self, query)
sqlite3.OperationalError: unsupported file format
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/usr/lib/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "/home/peace/.local/lib/python3.6/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/home/peace/.local/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 120, in inner_run
self.check_migrations()
File "/home/peace/.local/lib/python3.6/site-packages/django/core/management/base.py", line 458, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "/home/peace/.local/lib/python3.6/site-packages/django/db/migrations/executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "/home/peace/.local/lib/python3.6/site-packages/django/db/migrations/loader.py", line 49, in __init__
self.build_graph()
File "/home/peace/.local/lib/python3.6/site-packages/django/db/migrations/loader.py", line 212, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/home/peace/.local/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 76, in applied_migrations
if self.has_table():
File "/home/peace/.local/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 56, in has_table
return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
File "/home/peace/.local/lib/python3.6/site-packages/django/db/backends/base/introspection.py", line 48, in table_names
return get_names(cursor)
File "/home/peace/.local/lib/python3.6/site-packages/django/db/backends/base/introspection.py", line 43, in get_names
return sorted(ti.name for ti in self.get_table_list(cursor)
File "/home/peace/.local/lib/python3.6/site-packages/django/db/backends/sqlite3/introspection.py", line 73, in get_table_list
ORDER BY name""")
File "/home/peace/.local/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/home/peace/.local/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/peace/.local/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/peace/.local/lib/python3.6/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/home/peace/.local/lib/python3.6/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/peace/.local/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql)
File "/home/peace/.local/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 394, in execute
return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: unsupported file format
I'm just not really sure if I should change something since there is an unsupported file format. Just not even sure about the file it's talking about :(
Is it a bug or there is something I'm doing wrong?
I would recommend deleting your sqlite3 and migrations folders and then run the following:
python3 manage.py makemigrations <app>
python3 manage.py migrate
python3 manage.py runserver
You may refer to this link for deleting migration: https://riptutorial.com/django/example/29416/resetting-django-migration--deleting-existing-database-and-migrating-as-fresh
I just deleted my localhost database due to export my production website database and then import it to my lcoal db
I used this command to dump db:
./manage.py dumpdata --exclude contenttypes --exclude auth.permission
--exclude sessions --indent 2 > dump.json
and then on my localhost:
./manage.py loaddata dump.json
but I get error:
Traceback (most recent call last):
File "/home/mehdi/python/projects/FollowBotWebsite/v1.2.2/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/home/mehdi/python/projects/FollowBotWebsite/v1.2.2/venv/lib/python3.8/site-packages/django/db/backends/mysql/base.py", line 74, in execute
return self.cursor.execute(query, args)
File "/home/mehdi/python/projects/FollowBotWebsite/v1.2.2/venv/lib/python3.8/site-packages/MySQLdb/cursors.py", line 209, in execute
res = self._query(query)
File "/home/mehdi/python/projects/FollowBotWebsite/v1.2.2/venv/lib/python3.8/site-packages/MySQLdb/cursors.py", line 315, in _query
db.query(q)
File "/home/mehdi/python/projects/FollowBotWebsite/v1.2.2/venv/lib/python3.8/site-packages/MySQLdb/connections.py", line 239, in query
_mysql.connection.query(self, query)
MySQLdb._exceptions.IntegrityError: (1062, "Duplicate entry '1' for key 'user_id'")
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 "/home/mehdi/python/projects/FollowBotWebsite/v1.2.2/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/mehdi/python/projects/FollowBotWebsite/v1.2.2/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/mehdi/python/projects/FollowBotWebsite/v1.2.2/venv/lib/python3.8/site-packages/django/core/management/base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/mehdi/python/projects/FollowBotWebsite/v1.2.2/venv/lib/python3.8/site-packages/django/core/management/base.py", line 369, in execute
output = self.handle(*args, **options)
File "/home/mehdi/python/projects/FollowBotWebsite/v1.2.2/venv/lib/python3.8/site-packages/django/core/management/commands/loaddata.py", line 72, in handle
self.loaddata(fixture_labels)
File "/home/mehdi/python/projects/FollowBotWebsite/v1.2.2/venv/lib/python3.8/site-packages/django/core/management/commands/loaddata.py", line 114, in loaddata
self.load_label(fixture_label)
File "/home/mehdi/python/projects/FollowBotWebsite/v1.2.2/venv/lib/python3.8/site-packages/django/core/management/commands/loaddata.py", line 181, in load_label
obj.save(using=self.using)
File "/home/mehdi/python/projects/FollowBotWebsite/v1.2.2/venv/lib/python3.8/site-packages/django/core/serializers/base.py", line 223, in save
models.Model.save_base(self.object, using=using, raw=True, **kwargs)
File "/home/mehdi/python/projects/FollowBotWebsite/v1.2.2/venv/lib/python3.8/site-packages/django/db/models/base.py", line 782, in save_base
updated = self._save_table(
File "/home/mehdi/python/projects/FollowBotWebsite/v1.2.2/venv/lib/python3.8/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 "/home/mehdi/python/projects/FollowBotWebsite/v1.2.2/venv/lib/python3.8/site-packages/django/db/models/base.py", line 924, in _do_insert
return manager._insert(
File "/home/mehdi/python/projects/FollowBotWebsite/v1.2.2/venv/lib/python3.8/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/mehdi/python/projects/FollowBotWebsite/v1.2.2/venv/lib/python3.8/site-packages/django/db/models/query.py", line 1204, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "/home/mehdi/python/projects/FollowBotWebsite/v1.2.2/venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1392, in execute_sql
cursor.execute(sql, params)
File "/home/mehdi/python/projects/FollowBotWebsite/v1.2.2/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/home/mehdi/python/projects/FollowBotWebsite/v1.2.2/venv/lib/python3.8/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/mehdi/python/projects/FollowBotWebsite/v1.2.2/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/mehdi/python/projects/FollowBotWebsite/v1.2.2/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/home/mehdi/python/projects/FollowBotWebsite/v1.2.2/venv/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/mehdi/python/projects/FollowBotWebsite/v1.2.2/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/home/mehdi/python/projects/FollowBotWebsite/v1.2.2/venv/lib/python3.8/site-packages/django/db/backends/mysql/base.py", line 74, in execute
return self.cursor.execute(query, args)
File "/home/mehdi/python/projects/FollowBotWebsite/v1.2.2/venv/lib/python3.8/site-packages/MySQLdb/cursors.py", line 209, in execute
res = self._query(query)
File "/home/mehdi/python/projects/FollowBotWebsite/v1.2.2/venv/lib/python3.8/site-packages/MySQLdb/cursors.py", line 315, in _query
db.query(q)
File "/home/mehdi/python/projects/FollowBotWebsite/v1.2.2/venv/lib/python3.8/site-packages/MySQLdb/connections.py", line 239, in query
_mysql.connection.query(self, query)
django.db.utils.IntegrityError: Problem installing fixture '/home/mehdi/python/projects/FollowBotWebsite/v1.2.2/dump.json': Could not load users.Profile(pk=1): (1062, "Duplicate entry '1' for key 'user_id'")
I searched a lot but all soloutions were about permissions or auth, not my own models.
I used signals to make a profile for each user on creation,
solved this problem with addign not raw to my signals:
#receiver(post_save, sender=User)
def create_profile(sender, instance, created, raw=False, **kwargs):
if created and not raw:
Profile.objects.create(user=instance)
print('Profile Created')
I'm sorry, I don't think the title of my question is comprehensible as I'm absolutely a noob trying to understand django after LPTHW. Anyway, I'm following a tutorial in setting up Django to use MariaDB. But unfortunately when I run python manage.py migrate I get humongous block of error message. Here's the block:
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/2.1/ref/databases/#mysql-sql-mode
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial...Traceback (most recent call last):
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/db/backends/utils.py", line 83, in _execute
return self.cursor.execute(sql)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 71, in execute
return self.cursor.execute(query, args)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 250, in execute
self.errorhandler(self, exc, value)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
raise errorvalue
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 247, in execute
res = self._query(query)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 412, in _query
rowcount = self._do_query(q)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 375, in _do_query
db.query(q)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/MySQLdb/connections.py", line 276, in query
_mysql.connection.query(self, query)
_mysql_exceptions.OperationalError: (1050, "Table 'django_content_type' already exists")
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/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/core/management/base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/core/management/base.py", line 353, in execute
output = self.handle(*args, **options)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 203, in handle
fake_initial=fake_initial,
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/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/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/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/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/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/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/db/migrations/operations/models.py", line 91, in database_forwards
schema_editor.create_model(model)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 312, in create_model
self.execute(sql, params or None)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 133, in execute
cursor.execute(sql, params)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/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/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/db/backends/utils.py", line 83, in _execute
return self.cursor.execute(sql)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 71, in execute
return self.cursor.execute(query, args)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 250, in execute
self.errorhandler(self, exc, value)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
raise errorvalue
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 247, in execute
res = self._query(query)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 412, in _query
rowcount = self._do_query(q)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 375, in _do_query
db.query(q)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/MySQLdb/connections.py", line 276, in query
_mysql.connection.query(self, query)
django.db.utils.OperationalError: (1050, "Table 'django_content_type' already exists")
Expecting another command to work after that error, I ran
python manage.py createsuperuser which returned me another block of error:
You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
Traceback (most recent call last):
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 71, in execute
return self.cursor.execute(query, args)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 250, in execute
self.errorhandler(self, exc, value)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
raise errorvalue
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 247, in execute
res = self._query(query)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 412, in _query
rowcount = self._do_query(q)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 375, in _do_query
db.query(q)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/MySQLdb/connections.py", line 276, in query
_mysql.connection.query(self, query)
_mysql_exceptions.ProgrammingError: (1146, "Table 'myproject.auth_user' doesn't 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 "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/core/management/base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 59, in execute
return super().execute(*args, **options)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/core/management/base.py", line 353, in execute
output = self.handle(*args, **options)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 93, in handle
default_username = get_default_username()
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/contrib/auth/management/__init__.py", line 136, in get_default_username
auth_app.User._default_manager.get(username=default_username)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/db/models/query.py", line 393, in get
num = len(clone)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/db/models/query.py", line 250, in __len__
self._fetch_all()
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/db/models/query.py", line 1183, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/db/models/query.py", line 54, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1061, in execute_sql
cursor.execute(sql, params)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/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/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 71, in execute
return self.cursor.execute(query, args)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 250, in execute
self.errorhandler(self, exc, value)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
raise errorvalue
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 247, in execute
res = self._query(query)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 412, in _query
rowcount = self._do_query(q)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 375, in _do_query
db.query(q)
File "/home/noob/Documents/projects/python/django/myproject/myprojectenv/lib/python3.5/site-packages/MySQLdb/connections.py", line 276, in query
_mysql.connection.query(self, query)
django.db.utils.ProgrammingError: (1146, "Table 'myproject.auth_user' doesn't exist")
Could you help me solve this?
I have a Django project that does some calculations in Javascript.
I am using Ghost.py to try and incorporate efficient tests of the Javascript calculations into the Django test suite:
from ghost.ext.django.test import GhostTestCase
class CalculationsTest(GhostTestCase):
def setUp(self):
page, resources = self.ghost.open('http://localhost:8081/_test/')
self.assertEqual(page.http_status, 200)
def test_frobnicate(self):
result, e_resources = self.ghost.evaluate('''
frobnicate(test_data, "argument");
''')
self.assertEqual(result, 1.204)
(where frobnicate() is a Javascript function on the test page.
This works very well if I run one test at a time.
If, however, I run django-admin.py test, I get
Traceback (most recent call last):
...
result = self.run_suite(suite)
File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/test/runner.py", line 113, in run_suite
).run(suite)
File "/usr/lib64/python2.7/unittest/runner.py", line 151, in run
test(result)
File "/usr/lib64/python2.7/unittest/suite.py", line 70, in __call__
return self.run(*args, **kwds)
File "/usr/lib64/python2.7/unittest/suite.py", line 108, in run
test(result)
File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/test/testcases.py", line 184, in __call__
super(SimpleTestCase, self).__call__(result)
File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/ghost/test.py", line 53, in __call__
self._post_teardown()
File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/test/testcases.py", line 796, in _post_teardown
self._fixture_teardown()
File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/test/testcases.py", line 817, in _fixture_teardown
inhibit_post_syncdb=self.available_apps is not None)
File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/core/management/__init__.py", line 159, in call_command
return klass.execute(*args, **defaults)
File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
output = self.handle(*args, **options)
File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/core/management/base.py", line 415, in handle
return self.handle_noargs(**options)
File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/core/management/commands/flush.py", line 81, in handle_noargs
self.emit_post_syncdb(verbosity, interactive, db)
File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/core/management/commands/flush.py", line 101, in emit_post_syncdb
emit_post_sync_signal(set(all_models), verbosity, interactive, database)
File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/core/management/sql.py", line 216, in emit_post_sync_signal
interactive=interactive, db=db)
File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 185, in send
response = receiver(signal=self, sender=sender, **named)
File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py", line 82, in create_permissions
ctype = ContentType.objects.db_manager(db).get_for_model(klass)
File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/contrib/contenttypes/models.py", line 47, in get_for_model
defaults = {'name': smart_text(opts.verbose_name_raw)},
File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/db/models/manager.py", line 154, in get_or_create
return self.get_queryset().get_or_create(**kwargs)
File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/db/models/query.py", line 388, in get_or_create
six.reraise(*exc_info)
File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/db/models/query.py", line 380, in get_or_create
obj.save(force_insert=True, using=self.db)
File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/db/transaction.py", line 305, in __exit__
connection.commit()
File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/db/backends/__init__.py", line 168, in commit
self._commit()
File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/db/backends/__init__.py", line 136, in _commit
return self.connection.commit()
File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/db/utils.py", line 99, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/carl/.virtualenvs/fecfc/lib/python2.7/site-packages/django/db/backends/__init__.py", line 136, in _commit
return self.connection.commit()
django.db.utils.OperationalError: cannot commit - no transaction is active
(running with django-nose gives weirder, inconsistent results)
Any clues on how to prevent this issue, which is currently standing in the way of CI?
I haven't used Ghost myself but for a similar test I had to use TransactionTestCase to get a similar test working. Could you try changing GhostTestCase and see if that's working?