我对模型进行了更改,并尝试使用以下命令迁移数据库:
python3 manage.py makemigrations
python3 manage.py migrate
我得到了以下输出:
vagrant@vagrant-ubuntu-trusty-64:/vagrant/grader$ python3 manage.py makemigrations
No changes detected
vagrant@vagrant-ubuntu-trusty-64:/vagrant/grader$ python3 manage.py migrate Operations to perform:
Apply all migrations: contenttypes, sessions, admin, auth, core
Running migrations:
Rendering model states... DONE
Applying core.0002_auto_20160103_0955...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 342, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/commands/migrate.py", line 200, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/executor.py", line 92, in migrate
self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/executor.py", line 198, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/migration.py", line 123, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards
field,
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/schema.py", line 382, in add_field
definition, params = self.column_sql(model, field, include_default=True)
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/schema.py", line 145, in column_sql
default_value = self.effective_default(field)
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/schema.py", line 210, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "/usr/local/lib/python3.4/dist-packages/django/db/models/fields/related.py", line 910, in get_db_prep_save
return self.target_field.get_db_prep_save(value, connection=connection)
File "/usr/local/lib/python3.4/dist-packages/django/db/models/fields/__init__.py", line 728, in get_db_prep_save
prepared=False)
File "/usr/local/lib/python3.4/dist-packages/django/db/models/fields/__init__.py", line 968, in get_db_prep_value
value = self.get_prep_value(value)
File "/usr/local/lib/python3.4/dist-packages/django/db/models/fields/__init__.py", line 976, in get_prep_value
return int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'datetime.datetime'
我不知道迁移为什么不起作用。我可以看出原因是某种类型错误,但不知道原因是什么。
我认为我会尝试完全清除数据库,因为我没有相关数据。我计划使用:
python3 manage.py flush
python3 manage.py makemigrations
python3 manage.py migrate
但得到以下输出:
vagrant@vagrant-ubuntu-trusty-64:/vagrant/grader$ python3 manage.py flush
You have requested a flush of the database.
This will IRREVERSIBLY DESTROY all data currently in the 'graderdb' database,
and return each table to an empty state.
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: yes
CommandError: Database graderdb couldn't be flushed. Possible reasons:
* The database isn't running or isn't configured correctly.
* At least one of the expected database tables doesn't exist.
* The SQL was invalid.
Hint: Look at the output of 'django-admin sqlflush'. That's the SQL this command wasn't able to run.
The full error: cannot truncate a table referenced in a foreign key constraint
DETAIL: Table "core_mark" references "core_student".
HINT: Truncate table "core_mark" at the same time, or use TRUNCATE ... CASCADE.
如何完全重置Django psql数据库?
答案 0 :(得分:0)
如消息所示,请运行@Column
以查看Django正在尝试运行的SQL命令。
检查命令中使用的所有表是否都存在于数据库中。