Given a newly created django project with the following installed apps:
给定一个新创建的django项目,其中包含以下已安装的应用程序:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'registration',
)
When I run ./manage.py migrate for the first time I get the following error:
当我第一次运行./manage.py migrate时出现以下错误:
Operations to perform:
Synchronize unmigrated apps: staticfiles, messages, registration
Apply all migrations: sessions, admin, auth, contenttypes
Synchronizing apps without migrations:
Creating tables...
Creating table registration_registrationprofile
Running deferred SQL...
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/tcosta/Virtualenvs/django_project/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/Users/tcosta/Virtualenvs/django_project/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/tcosta/Virtualenvs/django_project/lib/python2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/tcosta/Virtualenvs/django_project/lib/python2.7/site-packages/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
File "/Users/tcosta/Virtualenvs/django_project/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 179, in handle
created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
File "/Users/tcosta/Virtualenvs/django_project/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 317, in sync_apps
cursor.execute(statement)
File "/Users/tcosta/Virtualenvs/django_project/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Users/tcosta/Virtualenvs/django_project/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Users/tcosta/Virtualenvs/django_project/lib/python2.7/site-packages/django/db/utils.py", line 97, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/tcosta/Virtualenvs/django_project/lib/python2.7/site-packages/django/db/backends/utils.py", line 62, in execute
return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "auth_user" does not exist```
It seems Django is trying to create the registration tables before the user table.
似乎Django正试图在用户表之前创建注册表。
This erros does not happen if I comment the registration app and run migrate and then uncomment the registration app and run migrate again. However, that's not the right way of doing it, right?
如果我评论注册应用程序并运行迁移然后取消注释注册应用程序并再次运行迁移,则不会发生此错误。但是,这不是正确的做法,对吗?
5 个解决方案
#1
12
After updating my Django version, I got this error and fix as running these two lines:
更新我的Django版本后,我收到此错误并修复为运行这两行:
python manage.py migrate auth
python manage.py migrate
auth_user table inside auth model should run first I guess.
auth_user表在auth模型中应首先运行我猜。
#2
1
I think you needed to run:
我想你需要运行:
python manage.py syncdb
python manage.py syncdb
and potentially you need to setup some dependencies? Probably not necessary after syncdb.
您可能需要设置一些依赖项?在syncdb之后可能没必要。
South 1 style (Django < 1.7)
class Migration:
depends_on = (
("accounts", "0001"),
)
def forwards(self):
....
South 2 style (Django >= 1.7)
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [("accounts", "0001")]
#3
0
Once you create a migration for your app it will automatically add the necessary dependencies. So in this case just run ./manage.py makemigrations registration
.
为应用程序创建迁移后,它将自动添加必要的依赖项。所以在这种情况下只需运行./manage.py makemigrations注册。
Please check the registration/migrations/0001_initial.py file and you should see something like this:
请检查registration / migrations / 0001_initial.py文件,您应该看到如下内容:
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
This means you need to create migrations for all your apps with any kind of dependency.
这意味着您需要为具有任何类型依赖关系的所有应用程序创建迁移。
#4
0
I had this problem too, solved it by replacing old registration with one that includes pull #25:
我也有这个问题,用一个包含pull#25的旧注册替换它来解决它:
pip install git+https://github.com/macropin/django-registration.git@v1.2c0
#5
0
The problem is avoided when you do as Pedro Wagner suggests (auth_user error with Django 1.8 and syncdb / migrate):
当您按照Pedro Wagner的建议(Django 1.8和syncdb / migrate的auth_user错误)时,可以避免此问题:
Make sure that for all your apps, there exist initial migrations files by running:
确保对于所有应用程序,通过运行存在初始迁移文件:
manage.py makemigrations my_app
I'd do it not only for those that depend on auth because I think the problem is more general.
我不仅仅是那些依赖auth的人,因为我认为问题更为笼统。
The root cause for this behaviour seems to me that for some reason
出于某种原因,在我看来这种行为的根本原因
manage.py makemigrations
does not always create the initial migrations if they are not already there, contrary to:
如果它们尚未存在,则并不总是创建初始迁移,与以下情况相反:
manage.py makemigrations my_app
Unfortunately I cannot fathom the reasons for this asymmetry.
不幸的是,我无法理解这种不对称的原因。
#1
12
After updating my Django version, I got this error and fix as running these two lines:
更新我的Django版本后,我收到此错误并修复为运行这两行:
python manage.py migrate auth
python manage.py migrate
auth_user table inside auth model should run first I guess.
auth_user表在auth模型中应首先运行我猜。
#2
1
I think you needed to run:
我想你需要运行:
python manage.py syncdb
python manage.py syncdb
and potentially you need to setup some dependencies? Probably not necessary after syncdb.
您可能需要设置一些依赖项?在syncdb之后可能没必要。
South 1 style (Django < 1.7)
class Migration:
depends_on = (
("accounts", "0001"),
)
def forwards(self):
....
South 2 style (Django >= 1.7)
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [("accounts", "0001")]
#3
0
Once you create a migration for your app it will automatically add the necessary dependencies. So in this case just run ./manage.py makemigrations registration
.
为应用程序创建迁移后,它将自动添加必要的依赖项。所以在这种情况下只需运行./manage.py makemigrations注册。
Please check the registration/migrations/0001_initial.py file and you should see something like this:
请检查registration / migrations / 0001_initial.py文件,您应该看到如下内容:
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
This means you need to create migrations for all your apps with any kind of dependency.
这意味着您需要为具有任何类型依赖关系的所有应用程序创建迁移。
#4
0
I had this problem too, solved it by replacing old registration with one that includes pull #25:
我也有这个问题,用一个包含pull#25的旧注册替换它来解决它:
pip install git+https://github.com/macropin/django-registration.git@v1.2c0
#5
0
The problem is avoided when you do as Pedro Wagner suggests (auth_user error with Django 1.8 and syncdb / migrate):
当您按照Pedro Wagner的建议(Django 1.8和syncdb / migrate的auth_user错误)时,可以避免此问题:
Make sure that for all your apps, there exist initial migrations files by running:
确保对于所有应用程序,通过运行存在初始迁移文件:
manage.py makemigrations my_app
I'd do it not only for those that depend on auth because I think the problem is more general.
我不仅仅是那些依赖auth的人,因为我认为问题更为笼统。
The root cause for this behaviour seems to me that for some reason
出于某种原因,在我看来这种行为的根本原因
manage.py makemigrations
does not always create the initial migrations if they are not already there, contrary to:
如果它们尚未存在,则并不总是创建初始迁移,与以下情况相反:
manage.py makemigrations my_app
Unfortunately I cannot fathom the reasons for this asymmetry.
不幸的是,我无法理解这种不对称的原因。