![](/img/trans.png)
[英]Django: Trying to set user as a foreign key in migration raises a ValueError
[英]Custom user in django raises ValueError
即使這個簡單的例子也會拋出一個ValueError: Dependency on app with no migrations: myApp
python manage.py syncdb
期間的ValueError: Dependency on app with no migrations: myApp
對myApp / models.py
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
pass
settings.py
AUTH_USER_MODEL = 'myApp.User'
在django中運行./manage syncdb
== 1.6.5 <<有效
Creating tables ...
在django中運行./manage syncdb
== 1.7 <<中斷
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/bdhammel/Documents/web_development/tutorials/python_social_auth/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/Users/bdhammel/Documents/web_development/tutorials/python_social_auth/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/bdhammel/Documents/web_development/tutorials/python_social_auth/env/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Users/bdhammel/Documents/web_development/tutorials/python_social_auth/env/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/Users/bdhammel/Documents/web_development/tutorials/python_social_auth/env/lib/python2.7/site-packages/django/core/management/base.py", line 533, in handle
return self.handle_noargs(**options)
File "/Users/bdhammel/Documents/web_development/tutorials/python_social_auth/env/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 27, in handle_noargs
call_command("migrate", **options)
File "/Users/bdhammel/Documents/web_development/tutorials/python_social_auth/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 115, in call_command
return klass.execute(*args, **defaults)
File "/Users/bdhammel/Documents/web_development/tutorials/python_social_auth/env/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/Users/bdhammel/Documents/web_development/tutorials/python_social_auth/env/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 63, in handle
executor = MigrationExecutor(connection, self.migration_progress_callback)
File "/Users/bdhammel/Documents/web_development/tutorials/python_social_auth/env/lib/python2.7/site-packages/django/db/migrations/executor.py", line 17, in __init__
self.loader = MigrationLoader(self.connection)
File "/Users/bdhammel/Documents/web_development/tutorials/python_social_auth/env/lib/python2.7/site-packages/django/db/migrations/loader.py", line 48, in __init__
self.build_graph()
File "/Users/bdhammel/Documents/web_development/tutorials/python_social_auth/env/lib/python2.7/site-packages/django/db/migrations/loader.py", line 239, in build_graph
parent = self.check_key(parent, key[0])
File "/Users/bdhammel/Documents/web_development/tutorials/python_social_auth/env/lib/python2.7/site-packages/django/db/migrations/loader.py", line 163, in check_key
raise ValueError("Dependency on app with no migrations: %s" % key[0])
ValueError: Dependency on app with no migrations: myApp
我還沒有在1.7的文檔中找到任何說明這應該以1.6的方式完成的任何內容。 它確實看起來像其他人也有這個問題 ,但是因為運行./manage.py migrate --list
有沒有遇到過這個?
我想我在錯誤的地方尋找答案:
我通過運行: ./manage.py makemigrations myApp
解決了這個問題
(env)Bens-MacBook-Pro:social_auth bdhammel$ ./manage.py makemigrations myApp
Migrations for 'myApp':
0001_initial.py:
- Create model User
(env)Bens-MacBook-Pro:social_auth bdhammel$ python manage.py syncdb
Operations to perform:
Apply all migrations: sessions, admin, myApp, auth, default, contenttypes
Running migrations:
Applying contenttypes.0001_initial... FAKED
Applying auth.0001_initial... FAKED
Applying app.0001_initial... FAKED
Applying admin.0001_initial... FAKED
Applying default.0001_initial... FAKED
Applying sessions.0001_initial... FAKED
You have installed Django's auth system, and don't have any superusers defined.
Would you like to create one now? (yes/no): yes
通過https://docs.djangoproject.com/en/1.7/topics/migrations/#s-custom-fields
編輯
我應該使用python manage.py migrate
而不是python manage.py syncdb
。
https://docs.djangoproject.com/en/1.8/releases/1.7/#schema-migrations
syncdb已被棄用,並由migrate替換。 別擔心 - 對syncdb的調用仍然可以像以前一樣工作。
python3 manage.py makemigrations app1
python3 manage.py makemigrations app2
manage.py runserver
現在應該像魅力一樣工作
當我將應用程序移動到一個干凈的新項目而不刪除遷移時,這種情況發生在我身上。 如果您不關心遷移並從頭開始創建項目,請考慮從這些應用程序中刪除遷移。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.