如下创建的django模型
from django.db import models
class Vender(models.Model):
id =models.IntegerField(primary_key=True)
name = models.CharField(max_length=30)
class Meta:
db_table="vendor"
class car(models.Model):
id =models.IntegerField(primary_key=True)
Vender=models.ForeignKey(Vender)
carmodel =models.CharField(max_length=30)
class Meta:
db_table="car"
最初makemigration
和migrate
工作正常。然后我改变了一些模型字段和选项。
之后得到以下错误。我是Django的新手。
这种问题在生产中就会发生,我们如何解决现实交易数据的影响。
F:\Workspace\virtspace\demosrc>python manage.py migrate
Operations to perform:
Apply all migrations: admin, contenttypes, base, auth, sessions
Running migrations:
Applying base.0003_auto_20150110_2004...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "D:\Python27\lib\site-packages\django\core\management\__init__.py", line 385, in execute_from_command_line
utility.execute()
File "D:\Python27\lib\site-packages\django\core\management\__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:\Python27\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "D:\Python27\lib\site-packages\django\core\management\base.py", line 338, in execute
output = self.handle(*args, **options)
File "D:\Python27\lib\site-packages\django\core\management\commands\migrate.py", line 160, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
File "D:\Python27\lib\site-packages\django\db\migrations\executor.py", line 63, in migrate
self.apply_migration(migration, fake=fake)
File "D:\Python27\lib\site-packages\django\db\migrations\executor.py", line 97, in apply_migration
migration.apply(project_state, schema_editor)
File "D:\Python27\lib\site-packages\django\db\migrations\migration.py", line 107, in apply
operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
File "D:\Python27\lib\site-packages\django\db\migrations\operations\fields.py", line 84, in database_forwards
schema_editor.remove_field(from_model, from_model._meta.get_field_by_name(self.name)[0])
File "D:\Python27\lib\site-packages\django\db\backends\schema.py", line 439, in remove_field
self.execute(sql)
File "D:\Python27\lib\site-packages\django\db\backends\schema.py", line 99, in execute
cursor.execute(sql, params)
File "D:\Python27\lib\site-packages\django\db\backends\utils.py", line 81, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "D:\Python27\lib\site-packages\django\db\backends\utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "D:\Python27\lib\site-packages\django\db\utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "D:\Python27\lib\site-packages\django\db\backends\utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "D:\Python27\lib\site-packages\django\db\backends\oracle\base.py", line 916, in execute
return self.cursor.execute(query, self._param_generator(params))
django.db.utils.DatabaseError: ORA-00904: "ID": invalid identifier
答案 0 :(得分:0)
id =models.IntegerField(primary_key=True)
django为你添加它。别担心。
删除这些字段makemigrations
,migrate
,您就可以了。
答案 1 :(得分:0)
可以保留
id=models.IntegerField(primary_key=True)
我多次遇到过这个问题,我使用的解决方案是,在更改现有模型的字段之前,运行:
python manage.py makemigrations --appName
python manage.py migrate --appName --fake
然后在模型中进行字段更改,运行如下:
python manage.py makemigrations --appName
python manage.py migrate --appName
你会看到所有绿色结果!
然后推理它因为:
当你运行makemigrations --appName
时,你可能会得到类似的东西:
Migrations for 'app':
0001_initial.py:
-Create model Vender
- Create model car
但实际上,当你第一次创建数据库时,你已经创建了这两个模型,所以只是以假的方式进行迁移,它只会保留初始文件。然后进行更改,再次运行迁移,系统将自动检测更改