在Postgres / PostGIS中迁移django模型没有反映

时间:2017-10-11 01:49:23

标签: python django postgresql postgis geodjango

我使用ogr2​​ogr成功将我的Geojson数据导入PostGIS。

我运行以下命令:python manage.py inspectdb

class Route(models.Model):
   ogc_fid = models.AutoField(primary_key=True)
   distance = models.FloatField(blank=True, null=True)
   route_type = models.CharField(max_length=-1, blank=True, null=True)
   route_long = models.CharField(max_length=-1, blank=True, null=True)
   route_name = models.CharField(max_length=-1, blank=True, null=True)
   agency_id = models.ForeignKey(max_length=-1, blank=True, null=True)
   route_id = models.CharField(max_length=-1, blank=True, null=True)
   route_url = models.CharField(max_length=-1, blank=True, null=True)
   route_desc = models.CharField(max_length=500, blank=True, null=True)
   duration = models.FloatField(blank=True, null=True)
   shape_id = models.IntegerField(blank=True, null=True)
   route_tcolor = models.CharField(max_length=-1, blank=True, null=True)
   route_color = models.CharField(max_length=-1, blank=True, null=True)
   wkb_geometry = models.LineStringField(blank=True, null=True)

class Meta:
    managed = False
    db_table = 'network_route'

当然,我必须将max_length修改为正整数,并将manage设置为True,所以我做了。

我还添加了一个额外的列created_atupdated_at

产生以下最终模型:

class Route(models.Model):
   ogc_fid = models.AutoField(primary_key=True)
   distance = models.FloatField(blank=True, null=True)
   route_type = models.CharField(max_length=100, blank=True, null=True)
   route_long = models.CharField(max_length=200, blank=True, null=True)
   route_name = models.CharField(max_length=100, blank=True, null=True)
   agency_id = models.ForeignKey(Agency, on_delete=models.CASCADE)
   route_id = models.CharField(max_length=100, blank=True, null=True)
   route_url = models.CharField(max_length=300, blank=True, null=True)
   route_desc = models.CharField(max_length=500, blank=True, null=True)
   duration = models.FloatField(blank=True, null=True)
   shape_id = models.IntegerField(blank=True, null=True)
   route_tcolor = models.CharField(max_length=100, blank=True, null=True)
   route_color = models.CharField(max_length=100, blank=True, null=True)
   wkb_geometry = models.LineStringField(blank=True, null=True)

   created_at = models.DateTimeField(default=timezone.now(),editable=False)
   updated_at = models.DateTimeField(blank=True, null=True)

   objects = models.GeoManager()

class Meta:
    managed = True
    db_table = 'network_route'

def save(self, *args, **kwargs):
    #On save, update timestamps
    if not self.id:
        self.created_at = timezone.now()
    self.updated_at = timezone.now()
    return super(Route, self).save(*args, **kwargs)

def __str__(self):
    return self.route_id

我运行:makemigrations [appname],它只检测Meta - Change Meta options on route

中的更改

如何在模型中添加其他更改,例如迁移所捕获的created_atupdated_at

我做错了吗?

1 个答案:

答案 0 :(得分:0)

问题必定发生在遗留代码中的那一行:managed = False。关于如何解决问题,有一些选择:

  1. 简单(干净)的方式:

    由于您可以重建数据库并且没有任何您不能没有的东西,因此请擦除所有内容并进行操作。

    • 删除迁移文件夹中__init__.py以外的所有内容。
    • 删除您的数据库并重新制作。
    • ./manage.py makemigrations
    • ./manage.py migrate

    嗯,你会失去一切,但这是“干净的石板”方法。

  2. (可能)更好的方式:

    打开您的上一次迁移(使用makemigrations命令创建的迁移)并手动编写其余部分:

    # -*- coding: utf-8 -*-
    from __future__ import unicode_literals
    
    import django.utils.timezone
    from django.db import migrations, models
    
    
    class Migration(migrations.Migration):
        dependencies = [
            ('your_app', 'your_previous_migration'),
        ]
    
        operations = [
            # That takes care of you Meta changes 
            migrations.AlterModelOptions(
                name='route',
                options={
                    'managed': True, 
                    'table': 'network_route', 
                },
            ),
            # That will add the created_at field
            migrations.AddField(
                model_name='route',
                name='created_at',
                field=models.DateTimeField(
                    default=django.utils.timezone.now,
                    editable=False
                ),
            ),
            # That will add the updated_at field
            migrations.AddField(
                model_name='route',
                name='updated_at',
                field=models.DateTimeField(blank=True, null=True),
       ]
    

    现在你可以尝试./manage.py migrate进行迁移,这应该可以解决问题!