添加新字段时,Django migratons失败

时间:2018-09-27 19:33:57

标签: django postgresql

我正尝试在另一个服务器的postgreSql数据库中添加一个新字段。在models.py

的ReservaSimple中添加名为“ aceptada”的新字段时

应用更改后,我使用了

python3 manage.py makemigrations

python3 manage.py migrate

但是,它表示未检测到任何更改,因此我不应该重新上传模型。 Migration error

我试图删除数据库并使用新的model.py文件使用新属性再次迁移,但是当我进行迁移时,没有更改应用于postgreSql的列。我也尝试删除应用中的迁移目录,然后再次迁移并手动创建列。但是,在运行服务器时,我总是会收到此错误。 server error

编辑:

我尝试执行以下步骤:

删除数据库

kafka_db=# DROP DATABASE final_db;
DROP DATABASE

从models.py中删除添加的字段

class ReservaSimple(models.Model):
    fecha_inicio = models.DateField(null = False)
    fecha_fin = models.DateField(null = False)
    comentarios = models.CharField(null = False, blank = True, max_length = 50)
    costo_final = models.FloatField(null = True)

从serializers.py中删除添加的字段

class ReservaSimpleSerializer(serializers.ModelSerializer):
    class Meta:
            model = ReservaSimple
            fields = ('fecha_inicio', 'fecha_fin', 'comentarios', 'costo_final')

创建数据库

kafka_db=# CREATE DATABASE final_db OWNER ROOT;
CREATE DATABASE

运行迁移

python3 manage.py migrate

响应

Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying aplicacion.0001_initial... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying sessions.0001_initial... OK

再次将该字段添加到models.py

class ReservaSimple(models.Model):
    fecha_inicio = models.DateField(null = False)
    fecha_fin = models.DateField(null = False)
    comentarios = models.CharField(null = False, blank = True, max_length = 50)
    costo_final = models.FloatField(null = True)
    aceptada = models.IntegerField(null = True)

将该字段添加到serializers.py

class ReservaSimpleSerializer(serializers.ModelSerializer):
    class Meta:
            model = ReservaSimple
            fields = ('fecha_inicio', 'fecha_fin', 'comentarios', 'costo_final', 'aceptada')

进行了迁移

m.leona@ISIS2503-015:~/nidooFinal$ python3 manage.py makemigrations
/usr/local/lib/python3.5/dist-packages/django/db/models/base.py:299: 
RuntimeWarning: Model 'aplicacion.reservasimple' was already registered. 
Reloading models is not advised as it can lead to inconsistencies, most 
notably 
with related models.
new_class._meta.apps.register_model(new_class._meta.app_label, new_class)
No changes detected 

运行迁移

Operations to perform:
Apply all migrations: admin, aplicacion, auth, contenttypes, sessions
Running migrations:
No migrations to apply.

从表格中选择*

final_db=# select * from aplicacion_reservasimple;
id | fecha_inicio | fecha_fin | comentarios | costo_final 
----+--------------+-----------+-------------+-------------
(0 rows)

final_db=# 

0 个答案:

没有答案