ForeignKey问题。 Django的迁移

时间:2019-02-04 13:18:54

标签: django django-models foreign-keys primary-key migrate

我必须定义code.spatial类,例如primary_key和codels.LScharacteristic codels.PlannedUsing如外键。 当我尝试迁移时,我遇到了这个问题:ProgrammingError:ERROR:外键约束中指定的id列不存在。

UPD完整日志:上面的异常是以下异常的直接原因:

回溯(最近通话最近):   在第23行的文件“ manage.py”中     execute_from_command_line(sys.argv)   第371行的文件“ /home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/core/management/init.py”,在execute_from_command_line中     utility.execute()   在执行的文件“ /home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/core/management/init.py”中执行     self.fetch_command(子命令).run_from_argv(self.argv)   在run_from_argv中的第288行,文件“ /home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/core/management/base.py”     self.execute(* args,** cmd_options)   在执行中,文件“ /home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/core/management/base.py”,第335行     输出= self.handle(* args,** options)   句柄中的文件“ /home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/core/management/commands/migrate.py”     fake_initial = fake_initial,   在迁移中,文件“ /home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/db/migrations/executor.py”,第117行     状态=自我._migrate_all_forwards(状态,计划,完整计划,假=假,假初始=假初始)   _migrate_all_forwards中的文件“ /home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/db/migrations/executor.py”,第147行     状态= self.apply_migration(状态,迁移,fake = fake,fake_initial = fake_initial)   在apply_migration中的第244行,文件“ /home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/db/migrations/executor.py”     状态= migration.apply(状态,schema_editor)   文件“ /home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/db/migrations/migration.py”,第122行     operation.database_forwards(self.app_label,schema_editor,old_state,project_state)   文件“ /home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/db/migrations/operations/fields.py”,第216行,在database_forwards中     schema_editor.alter_field(from_model,from_field,to_field)   在alter_field中的文件“ /home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/db/backends/base/schema.py”,第525行     old_db_params,new_db_params,严格)   文件“ /home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/db/backends/postgresql/schema.py”,第122行,位于_alter_field中     new_db_params,严格,   文件“ /home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/db/backends/base/schema.py”,第750行,位于_alter_field中     self.execute(self._create_fk_sql(model,new_field,“ fk %(to_table)s _%(to_column)s”))   在执行中,文件“ /home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/db/backends/base/schema.py”     cursor.execute(sql,params)   执行中的文件“ /home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/db/backends/utils.py”     返回super()。execute(sql,params)   执行中的文件“ /home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/db/backends/utils.py”,第68行     返回self._execute_with_wrappers(sql,params,many = False,executor = self._execute)   _execute_with_wrappers中的文件“ /home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/db/backends/utils.py”,第77行     返回执行器(sql,params,许多上下文)   _execute中的文件“ /home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/db/backends/utils.py”,第85行     返回self.cursor.execute(sql,params)   退出中的文件“ /home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/db/utils.py”,第89行     从exc_value提高dj_exc_value.with_traceback(traceback)   _execute中的文件“ /home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/db/backends/utils.py”,第85行     返回self.cursor.execute(sql,params)

但是我的模型中没有id列:

from django.db import models
from django.utils.translation import ugettext_lazy as _
from renter.models import RefAbstract, Renter
from django.contrib.gis.db import models
from textwrap import shorten


class Unitt(RefAbstract):
   ....//some classes
class Task(RefAbstract):
    class Meta(RefAbstract.Meta):
        verbose_name = 'task'
        verbose_name_plural = 'tasks'

class Spatial(models.Model):
    codeq = models.IntegerField('no',help_text='no') 
    code = models.PositiveIntegerField('cutare',primary_key=True,help_text='cutare')//unique column
    codeV = models.IntegerField('novi',help_text='novi') 
    renter = models.ForeignKey(Renter, on_delete=models.DO_NOTHING, verbose_name='renter')
    geometry = models.MultiPolygonField(geography=True, verbose_name='geometry')

    class Meta:
        verbose_name = 'cutarea'
        verbose_name_plural = 'cutarea'


class LScharacteristic(models.Model):
    codels = models.ForeignKey(Spatial, on_delete=models.DO_NOTHING, verbose_name = 'cutarea')// Foreign Key
    tract = models.CharField('tract',max_length = 80, help_text='tract') 
    task = models.ForeignKey(Task,  on_delete=models.DO_NOTHING, verbose_name='task')
    totalarea = models.PositiveIntegerField('totarea',help_text = 'totarea')
    explarea = models.PositiveIntegerField('exarea',help_text = 'exarea')
    protecttype = models.CharField('category',max_length = 50, help_text = 'category')

    class Meta:
        verbose_name = 'characteristic'
        verbose_name_plural = 'characteristics'

class PlannedUsing(models.Model):
    codels = models.ForeignKey(Spatial, on_delete=models.DO_NOTHING, verbose_name = 'cutarea') // Foreign Key   
    codeq = models.IntegerField(help_text='number')
    cutareaShape = models.ForeignKey(CutareaShape, on_delete=models.DO_NOTHING, verbose_name='form')
    cuttype = models.ForeignKey(CutareaType, on_delete=models.DO_NOTHING, verbose_name='type1')
    managetype = models.ForeignKey(ManageType, on_delete=models.DO_NOTHING, verbose_name='type2')
    unit = models.ForeignKey(Unitt, on_delete=models.DO_NOTHING, verbose_name='unit')
    composition = models.ForeignKey(Composition, on_delete=models.DO_NOTHING, verbose_name='sort')
    assortment = models.ForeignKey(Assortment, on_delete=models.DO_NOTHING, verbose_name='assort)
    class Meta:
        verbose_name = 'planus'
        verbose_name_plural = 'planuss'

什么是id列?如果我没有定义ID列,为什么存在ID列? 我该如何解决?

2 个答案:

答案 0 :(得分:0)

您可以使用

def __str__(self):
        return self.etat

def __int__(self):

具体来说,您将在Django模型上返回什么。 像这样:

tests= models.CharField(max_length=100, verbose_name="Test")
class Meta:
        verbose_name = "Tests"
        ordering = ['tests']

    def __str__(self):
        return self.tests

答案 1 :(得分:0)

我认为问题在于您的ForeignKey字段未明确指向您已标记为主要(Spatial.code)的字段。 通过将Spatial.code标记为主要,可以防止Django创建id字段。请参见the documentation,但如果将ForeignKey指向某个模型,它将尝试链接到该模型的id字段。 要解决此问题,您可以在参数ForeignKey字段中添加to_field='code'并将unique=True添加到Spatial.codeSee documentation