Python Django 1.8 和 syncdb / migrate 的 auth_user 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29689365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
auth_user error with Django 1.8 and syncdb / migrate
提问by mpso
When upgrading to Django 1.8 (with zc.buildout) and running syncdb or migrate, I get this message:
升级到 Django 1.8(使用 zc.buildout)并运行 syncdb 或 migrate 时,我收到以下消息:
django.db.utils.ProgrammingError: relation "auth_user" does not exist
django.db.utils.ProgrammingError: relation "auth_user" does not exist
One of my models contains django.contrib.auth.models.User:
我的模型之一包含 django.contrib.auth.models.User:
user = models.ForeignKey(
User, related_name='%(app_label)s_%(class)s_user',
blank=True, null=True, editable=False
)
Downgrading to Django 1.7 removes the error. Do I have to include the User object differently in Django 1.8?
降级到 Django 1.7 会消除错误。我是否必须在 Django 1.8 中以不同的方式包含 User 对象?
回答by Rod Xavier
Try referring to the User using this
尝试使用此引用用户
from django.conf import settings
user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='%(app_label)s_%(class)s_user', blank=True, null=True, editable=False)
回答by radtek
To fix this problem here is what I did:
为了解决这个问题,我是这样做的:
1) Find all foreign key relation fields like OneToOneField, ForeignKey and ManyToManyFields in your project, including any reusable apps that are referring to auth.User
or import User and set it to settings.AUTH_USER_MODEL as above. At minimum use:
1) 在您的项目中找到所有外键关系字段,如 OneToOneField、ForeignKey 和 ManyToManyFields,包括引用auth.User
或导入 User 的任何可重用应用程序并将其设置为 settings.AUTH_USER_MODEL 如上所述。最少使用:
'auth.User'
2) For all the models that have the above, make sure the models have a valid django migration (not south). If they have south migrations, rename the directory to migrations_south and then run the makemigrations command for that app:
2)对于所有具有上述功能的模型,请确保模型具有有效的 django 迁移(不是向南)。如果他们有南迁移,请将目录重命名为 migrations_south,然后为该应用程序运行 makemigrations 命令:
./manage.py makemigrations affected_app
Sometimes there is a django migrations folder under a different name, not the default migrations
directory. In such cases, reference this via MIGRATION_MODULES
in your settings.py:
有时会有不同名称的 django migrations 文件夹,而不是默认migrations
目录。在这种情况下,请通过MIGRATION_MODULES
settings.py 中的引用:
MIGRATION_MODULES = {'filer': 'filer.migrations_django'}
Since the issue was hard to find on larger projects, I commented out all custom apps in INSTALLED_APPS
in settings.py and ran the test command, since it will run migrate and try to re-create the database for you:
由于在较大的项目中很难找到该问题,因此我INSTALLED_APPS
在 settings.py 中注释掉了所有自定义应用程序并运行了测试命令,因为它将运行 migrate 并尝试为您重新创建数据库:
./manage.py test
Looks like that fixed it for me. I'm not sure if step 1 is mandatory or just best practice. But you definitely need to convert the apps to migrations.
看起来那为我修好了。我不确定第 1 步是强制性的还是只是最佳实践。但是您肯定需要将应用程序转换为迁移。
Cheers!
干杯!
PS. Be ready for what's coming in Django 1.9. The syncdb command will be removed. The legacy method of syncing apps without migrations is removed, and migrations are compulsory for all apps.
附注。准备好迎接Django 1.9的到来。syncdb 命令将被删除。删除了无需迁移即可同步应用程序的旧方法,并且所有应用程序都必须进行迁移。
回答by Dave Lawrence
I fix this by running auth first, then the rest of my migrations:
我通过先运行 auth 来解决这个问题,然后是其余的迁移:
python manage.py migrate auth
python manage.py migrate
回答by Pedro Vagner
On my environment, I fix this running makemigrations
on all apps that have relationship with django.contrib.auth.models
:
在我的环境中,我修复了makemigrations
在所有与django.contrib.auth.models
以下相关的应用程序上运行的问题:
manage.py makemigrations app_with_user_relation manage.py migrate
回答by Aaron F
If you are using heroku like I was run
如果你像我一样使用 heroku
heroku run python manage.py makemigrations
This will likely give you a message saying there are now changes. Ignore that then run
这可能会给你一条消息,说现在有变化。忽略然后运行
heroku run python manage.py migrate
this will give you some output suggesting something has been done. Finally run
这将为您提供一些输出,表明已完成某些操作。最后运行
heroku run python manage.py createsuperuser
回答by Jastatham
I also had the same issue I solved it by using these :
我也有同样的问题,我通过使用这些解决了它:
python manage.py migrate auth
python manage.py migrate
Then migrate does its job
然后迁移完成它的工作
回答by Ytsen de Boer
This problem is contained by running "makemigrations" for all apps, that have not yet been migrated, i.e. apps that do not yet have an "initial_0001.py" file in their migrations directory.
这个问题是通过为所有尚未迁移的应用程序运行“makemigrations”来解决的,即在其迁移目录中还没有“initial_0001.py”文件的应用程序。
This is done (in our case we use a makefile) by running for each of these apps:
这是通过为每个应用程序运行来完成的(在我们的例子中,我们使用 makefile):
manage.py makemigrations app_name
Once that is done, you can execute:
完成后,您可以执行:
manage.py migrate
as usual.
照常。
The underlying cause for this is that for some reason
造成这种情况的根本原因是出于某种原因
manage.py makemigrations
does notalways create these initial migrations if they are not already there. And that leads to the mentioned error.
如果这些初始迁移尚未存在,则并不总是创建这些初始迁移。这导致了上述错误。
On the contrary,
相反,
manage.py makemigrations app_name
doesalways create them (if not already there). Unfortunately I cannot fathom the reasons for this asymmetry.
确实总是创建它们(如果还没有)。不幸的是,我无法理解这种不对称的原因。
回答by Sebastian Wagner
I've migrated an old Django 1.6 project to Django 1.8 and previously we've used syncdb to migrate the database and we did not have initial migration stepsfor all apps in our project. With Django 1.8, you'll need a working database migrations. Running
我已经将一个旧的 Django 1.6 项目迁移到 Django 1.8,之前我们使用了 syncdb 来迁移数据库,并且我们没有针对项目中的所有应用程序进行初始迁移步骤。使用 Django 1.8,您将需要一个有效的数据库迁移。跑步
manage.py makemigrations <app_name>
for all apps in our project fixed our problems.
对于我们项目中的所有应用程序都解决了我们的问题。
回答by khaled kachkorian
Maybe you have found the answer and resolved the problem, but I wanted to point out that, in my case, the above problem was solved by dropping the database and re-creating it again, with the full privileges of the users. I was able to do this because I'm working on a non-production environment, but doing this on a staging environment is not a good idea, so be careful.
也许您已经找到了答案并解决了问题,但我想指出的是,就我而言,上述问题是通过删除数据库并以用户的完全权限重新创建它来解决的。我能够这样做是因为我在非生产环境中工作,但在临时环境中这样做不是一个好主意,所以要小心。
Im using python 2.7.12
and following are the specifications of my virtualenv:
我正在使用python 2.7.12
和以下是我的 virtualenv 的规格:
Django==1.10.5
django-crispy-forms==1.6.1
django-registration-redux==1.4
djangorestframework==3.5.3
olefile==0.44
packaging==16.8
Pillow==4.0.0
psycopg2==2.6.2