When I issue the command:
python manage.py syncdb --database=mydb
It shows the output as follows...
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table django_content_type
Traceback (most recent call last):
File "manage.py", line 14, in <module>
execute_manager(settings)
File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 459, in execute_manager
utility.execute()
File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
output = self.handle(*args, **options)
File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 371, in handle
return self.handle_noargs(**options)
File "/usr/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 110, in handle_noargs
emit_post_sync_signal(created_models, verbosity, interactive, db)
File "/usr/lib/python2.7/site-packages/django/core/management/sql.py", line 189, in emit_post_sync_signal
interactive=interactive, db=db)
File "/usr/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 172, in send
response = receiver(signal=self, sender=sender, **named)
File "/usr/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py", line 35, in create_permissions
ctype = ContentType.objects.get_for_model(klass)
File "/usr/lib/python2.7/site-packages/django/contrib/contenttypes/models.py", line 42, in get_for_model
defaults = {'name': smart_unicode(opts.verbose_name_raw)},
File "/usr/lib/python2.7/site-packages/django/db/models/manager.py", line 134, in get_or_create
return self.get_query_set().get_or_create(**kwargs)
File "/usr/lib/python2.7/site-packages/django/db/models/query.py", line 442, in get_or_create
return self.get(**lookup), False
File "/usr/lib/python2.7/site-packages/django/db/models/query.py", line 361, in get
num = len(clone)
File "/usr/lib/python2.7/site-packages/django/db/models/query.py", line 85, in __len__
self._result_cache = list(self.iterator())
File "/usr/lib/python2.7/site-packages/django/db/models/query.py", line 291, in iterator
for row in compiler.results_iter():
File "/usr/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 763, in results_iter
for rows in self.execute_sql(MULTI):
File "/usr/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 818, in execute_sql
cursor.execute(sql, params)
File "/usr/lib/python2.7/site-packages/django/db/backends/util.py", line 40, in execute
return self.cursor.execute(sql, params)
File "/usr/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 337, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.DatabaseError: no such table: django_content_type
I have a custom db router that is basically set to django's example, except i have a custom attribute on MY models so that it knows which database they default to. syncdb works when in my settings.py INSTALLED_APPS I comment out: django.contrib.auth and django.contrib.contenttypes. Had this problem for a while, but have been putting it off until now, when I need to start on authentication. If you want my db router I'll post also
I've explained a similar problem here:
django.db.utils.IntegrityError: (1062, "Duplicate entry '22-add_' for key 'content_type_id'")
You don't need to comment out contrib.auth and contrib.contenttypes. Just make sure all django models - users, sessions, permissions are only used in 1 database, which could be considered as a master.
This will not directly solve your problem, but can be a starting point when dealing with multiple db's and db routers. What you need to know is that each model has it's content type in the database.
The problem occur when django objects - user/session/permission are not restricted to a single database - then they are created into each database. And since a content-type makes a model unique, having content-types for a single type in multiple databases could lead to the problem explained in the other SO question above.
In Django 1.4 you have to launch this command in the top-level folder of the project (path_to_project/you_project NOT path_to_project/you_project/you_project)
Related
In my multi-tenant site, I have the following database structure:
Public (shared):
tenants
domains
users
Test:
users
clients
projects
projectusers
tasks
Test2:
users
clients
projects
projectusers
tasks
This is a simplified list - all three schemas include (among others) auth_permissions, auth_group, and auth_group_permissions.
The projectusers table (cross-ref that ties users to projects), has a One-to-One field for the User table, and a ForeignKey to the projects table, as follows:
class ProjectUser(DateFieldsModelMixin, models.Model):
user = models.OneToOneField(
User,
on_delete=models.PROTECT,
verbose_name=_('User'),
related_name='user_projects',
)
project = models.ForeignKey(
Project,
on_delete=models.PROTECT,
verbose_name=_('Project'),
related_name='project_users',
)
...other fields omitted...
The point is, the reference from projectusers to users is defined in the ProjectUser model. There's no reference from User model back to ProjectUser.
I wrote a load_fixtures (management) command to, well, load fixtures to set up our dev site(s) with the data we'd need to do preliminary testing (before writing/running unit-tests).
One of the things that command does is empty the auth_permissions table, reset the auto-increment value back to 1, and then import its fixture so that all IDs match across local dev sites, and (eventually) in production. This is necessary because fixtures for auth_group and auth_group_permissions reference permissions by ID.
The error I'm getting occurs during the delete process on that table when running in the "public" schema. Note there is no projectusers table in Public. This is the line that does the delete on auth_permissions:
model.objects.all().delete()
That is line 215 in the load_fixtures command, in the clear_table() method. You can see that line referenced in the traceback:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
output = self.handle(*args, **options)
File "/code/src/virticl_api/utilities/management/commands/load_fixtures.py", line 148, in handle
self.clear_table(file_path, self.clear_tables[file_name])
File "/code/src/virticl_api/utilities/management/commands/load_fixtures.py", line 215, in clear_table
model.objects.all().delete()
File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 662, in delete
collector.collect(del_query)
File "/usr/local/lib/python3.6/site-packages/django/db/models/deletion.py", line 220, in collect
elif sub_objs:
File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 272, in __bool__
self._fetch_all()
File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 1186, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/usr/local/lib/python3.6/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 "/usr/local/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1065, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/usr/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 "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "projects_projectuser" does not exist
LINE 1: ...ourly_rate", "projects_projectuser"."status" FROM "projects_...
So why is that delete method, running in the Public schema, finding a relationship to the projectuser table?
I've recently inherited a production codebase for a webapp written using Django. Up until now, the database the project has been using has been the default SQLite3 database, but now that more people are using the app, moving to Postgres is necessary.
I've been able to set up an empty postgres database with the project that works fine. The problem I'm encountering is in moving the data from the old project to the new one. I can run
python manage.py dumpdata --natural-foreign > dump.json
to dump the data, which works fine, but when I switch to postgres in settings.py and run
python manage.py loaddata dump.json
I get the following error:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 60, in handle
self.loaddata(fixture_labels)
File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 90, in loaddata
self.load_label(fixture_label)
File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 147, in load_label
obj.save(using=self.using)
File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/core/serializers/base.py", line 173, in save
models.Model.save_base(self.object, using=using, raw=True)
File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/db/models/base.py", line 738, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/db/models/base.py", line 803, in _save_table
forced_update)
File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/db/models/base.py", line 853, in _do_update
return filtered._update(values) > 0
File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/db/models/query.py", line 580, in _update
return query.get_compiler(self.db).execute_sql(CURSOR)
File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1062, in execute_sql
cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 840, in execute_sql
cursor.execute(sql, params)
File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/db/utils.py", line 97, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: Problem installing fixture '/mnt/d/Code/MCJobTrack/jobtrack_project/dump.json': Could not load contenttypes.ContentType(pk=15): duplicate key value violates unique constraint "django_content_type_app_label_45f3b1d93ec8c61c_uniq"
DETAIL: Key (app_label, model)=(jobtrack, purchaseorder) already exists.
So far I've tried:
Running TRUNCATE django_content_type RESTART IDENTITY CASCADE; in the dbshell
Excluding contenttypes when dumping the data. (I get the error django.core.serializers.base.DeserializationError: Problem installing fixture '/mnt/d/Code/MCJobTrack/jobtrack_project/dump_no_contenttypes.json': ContentType matching query does not exist.)
Resetting the primary key sequence
Using the --natural-primary tag when dumping the data
Any help would be greatly appreciated.
You probably need to use --natural-primary as well, so that the primary keys of content types are not exported to the fixture:
python manage.py dumpdata --natural-foreign --natural-primary > dump.json
Turns out there were two things I needed to do:
Dump the data with --natural-primary, --natural-foreign, and -e
contenttypes
Have loaddata ignore post_save signals
It still takes a very long time to load the data, but it works.
I am trying to unitest my models in my django application that consisits of some migrations. I have prepared my test, but when running
./manage.py test my_app
i get the following error
django.db.utils.OperationalError: (1005, "Can't create table 'test_rhombusdb.#sql-4ca_2c' (errno: 150)")
The database is created normally, So I guess I have permissions to create the db.
I really don't know which table this is reffering to. Migrations pass normal and db works fine in production. What could be the issue here?
what other info would you like?
my stack trace
File "./manage.py", line 18, 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/commands/test.py", line 30, in run_from_argv
super(Command, self).run_from_argv(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/commands/test.py", line 74, in execute
super(Command, self).execute(*args, **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/test.py", line 90, in handle
failures = test_runner.run_tests(test_labels)
File "/usr/local/lib/python2.7/dist-packages/django/test/runner.py", line 210, in run_tests
old_config = self.setup_databases()
File "/usr/local/lib/python2.7/dist-packages/django/test/runner.py", line 166, in setup_databases
**kwargs
File "/usr/local/lib/python2.7/dist-packages/django/test/runner.py", line 370, in setup_databases
serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True),
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/creation.py", line 368, in create_test_db
test_flush=not keepdb,
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 120, in call_command
return command.execute(*args, **defaults)
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 179, in handle
created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 317, in sync_apps
cursor.execute(statement)
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)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 124, in execute
return self.cursor.execute(query, args)
File "/usr/local/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "/usr/local/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
django.db.utils.OperationalError: (1005, "Can't create table 'testdb.#sql-4ca_7c' (errno: 150)")
I can see there is an error when migration is called.
PS: I droped my database(real one but the developement one) and tried to recreate tables running migrate. I came with the same error. Running migrate again worked like a charm....Something is really really really wrong here....
PS2: Deleting all migration folders and starting without gave me the same error after running migrate with no migration folders present....Very bad error very difficult to debug
PS3: There is an issue with the foreign key on one (at least so far i've tested) of my models. I tried deactivating apps and activating them in terms, and when I got to a specific one, error occured.I isolated one model, and gave me the issue. I commented out the foreign keys and worked. Uncomment one of them error again.
class Patient(models.Model):
#many fields here
doctor = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="patients")
It worked with 1.6 and sqlite3, I moved to 1.8 and mysql, and boom...error.
AUTH_USER_MODEL = 'users.MyCustomUser'
MyCustomUser is a class inheriting AbstractBaseUser and PermissionsMixin.
I had the same (pretty general) error message. For me it came down to having forgot to run makemigrations on a new app. See also:
https://docs.djangoproject.com/en/1.8/topics/migrations/#dependencies
I have a problem with Django quite incomprehensible.
I used models.py to model my database. This database runs on the production server. I want to change it, so I create a new database on the development server. The problem happens when I do syncdb, Django tries to create twice the same table, as you can see below:
python manage.py syncdb
Operations to perform:
Synchronize unmigrated apps: inventaryApp, newPrpvApp, grappelli, debug_toolbar, registration, import_export
Apply all migrations: sitetree, sessions, admin, sites, auth, contenttypes
Synchronizing apps without migrations:
Creating tables...
Creating table u_institutions
Creating table u_users
Creating table o_organes
Creating table o_degats
Creating table o_validation
Creating table o_m_determination
Creating table o_stades_dvlpmt
Creating table o_types
Creating table precision_date
Creating table t_categories
Creating table t_etat_synonymie
Creating table g_langues
Creating table g_pays
Creating table g_regions
Creating table g_communes
Creating table pays_langues
Creating table newprpvapp_userprofile
Creating table t_noms_verna
Creating table langues_noms_verna
Creating table maladies
Creating table t_familles
Creating table t_genres
Creating table t_especes
Creating table images
Creating table esp_noms_verna
Creating table t_infra_types
Creating table t_infra_sp
Creating table t_synonymes
Creating table o_contexte
Creating table o_interception
Creating table o_hotes
Creating table hotes_organes
Creating table o_organismes_associes
Creating table u_institutions
As you can see, the u_institutions table is created the first, but also at the end. And this is where the execution of the command stops to show:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/base.py", line 533, in handle
return self.handle_noargs(**options)
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 27, in handle_noargs
call_command("migrate", **options)
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 115, in call_command
return klass.execute(*args, **defaults)
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 128, in handle
created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 239, in sync_apps
cursor.execute(statement)
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 63, in execute
return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "u_institutions" already exists
I have verified that the table had not been set twice in model.py, and it was not. I use Django 1.7, and psycopg 2.6. Do you have an idea please? Thank you all!
Edit
I had already executed makemigrations, and everything was good. Now it indicates that no change is detected, which is probably normal.
When I try migrate, I get :
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 128, in handle
created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 239, in sync_apps
cursor.execute(statement)
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 63, in execute
return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "u_institutions" already exists
And migrate --fake return :
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 128, in handle
created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 239, in sync_apps
cursor.execute(statement)
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/hugo/DEV/.virtualenvs/env_eprpv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 63, in execute
return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "u_institutions" already exists
It seems that you have already created your database schema. Please, use migration (as long as you use Django 1.7). syncdb is deprecated.
python manage.py makemigrations
python manage.py migrate
If you will face any errors here (like relation "..." already exists) run this command to mark this tables as applied:
python manage.py migrate --fake
I am trying to setup a database for a django application.
So when i attempt to create database everything works fine except one thing. At the end, this message comes up:
You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no):
If i type "yes" and hit the enter key then it gives me this error:
D:\xampp\htdocs\Django>cd myapps
D:\xampp\htdocs\Django\myapps>python manage.py syncdb
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table django_admin_log
You just installed Django's auth system, which means you don't have any superuse
rs defined.
Would you like to create one now? (yes/no): yes
Traceback (most recent call last):
File "manage.py", line 14, in <module>
execute_manager(settings)
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
438, in execute_manager
utility.execute()
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 191,
in run_from_argv
self.execute(*args, **options.__dict__)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 220,
in execute
output = self.handle(*args, **options)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 351,
in handle
return self.handle_noargs(**options)
File "C:\Python27\lib\site-packages\django\core\management\commands\syncdb.py"
, line 109, in handle_noargs
emit_post_sync_signal(created_models, verbosity, interactive, db)
File "C:\Python27\lib\site-packages\django\core\management\sql.py", line 190,
in emit_post_sync_signal
interactive=interactive, db=db)
File "C:\Python27\lib\site-packages\django\dispatch\dispatcher.py", line 172,
in send
response = receiver(signal=self, sender=sender, **named)
File "C:\Python27\lib\site-packages\django\contrib\auth\management\__init__.py
", line 70, in create_superuser
call_command("createsuperuser", interactive=True)
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
166, in call_command
return klass.execute(*args, **defaults)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 220,
in execute
output = self.handle(*args, **options)
File "C:\Python27\lib\site-packages\django\contrib\auth\management\commands\cr
eatesuperuser.py", line 72, in handle
User.objects.get(username=default_username)
File "C:\Python27\lib\site-packages\django\db\models\manager.py", line 132, in
get
return self.get_query_set().get(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\query.py", line 344, in g
et
num = len(clone)
File "C:\Python27\lib\site-packages\django\db\models\query.py", line 82, in __
len__
self._result_cache = list(self.iterator())
File "C:\Python27\lib\site-packages\django\db\models\query.py", line 273, in i
terator
for row in compiler.results_iter():
File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 68
0, in results_iter
for rows in self.execute_sql(MULTI):
File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 73
5, in execute_sql
cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\util.py", line 34, in e
xecute
return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\base.py", line
234, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.DatabaseError: You must not use 8-bit bytestrings unless you use
a text_factory that can interpret 8-bit bytestrings (like text_factory = str).
It is highly recommended that you instead just switch your application to Unicod
e strings.
D:\xampp\htdocs\Django\myapps>
I believe it is supposed to prompt me to enter a username and password if i choose "yes" but it doesn't prompt me anyhting and give me this error.
Any suggestion?
It looks like this is due to a bug in Django related to system usernames that contain non-8-bit characters.
As a workaround, you can say "no" when asked to create a superuser and use the createsuperuser command to create a new superuser with a different name:
$ python manage.py syncdb
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site
You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): no
Installing index for auth.Permission model
Installing index for auth.Group_permissions model
Installing index for auth.User_user_permissions model
Installing index for auth.User_groups model
Installing index for auth.Message model
No fixtures found.
$ python manage.py createsuperuser --username yourname
E-mail address: email#example.com
Password:
Password (again):
Superuser created successfully.