Django:没有唯一约束匹配给定引用表的键#34;

时间:2018-01-24 11:01:24

标签: python django

有许多与此错误相关的问题,但经过一个多月我仍然无法解决此问题,因为在我的设置中实际上存在一个独特的约束。我之前已经问过这个问题,但答案是在我的模型中添加一个唯一的字段 IS 已经存在。

我正在运行较旧的django版本1.7.11,情况不允许我升级。

我有一个Photo类,其中包含多个其他类的外键。

class Photo(UserBase):
    """
    Photograph
    """

scene_category = models.ForeignKey(
        SceneCategory, related_name='photos', null=True, blank=True)
artist = models.ForeignKey(
        Artist, related_name='photos', null=True, blank=True)
gallery = models.ForeignKey(
        Gallery, related_name='photos', null=True, blank=True)

UserBase

的继承
class UserBase(ModelBase):

    #: User that created this object
    user = models.ForeignKey(UserProfile)

    class Meta:
        abstract = True
        ordering = ['-id']

class ModelBase(EmptyModelBase):
    """ Base class of all models, with an 'added' field """
    added = models.DateTimeField(default=now)

    class Meta:
        abstract = True
        ordering = ['-id']

class EmptyModelBase(models.Model):
    """ Base class of all models, with no fields """

    def get_entry_dict(self):
        return {'id': self.id}

    def get_entry_attr(self):
        ct = ContentType.objects.get_for_model(self)
        return 'data-model="%s/%s" data-id="%s"' % (
            ct.app_label, ct.model, self.id)

    def get_entry_id(self):
        ct = ContentType.objects.get_for_model(self)
        return '%s.%s.%s' % (ct.app_label, ct.model, self.id)

    class Meta:
        abstract = True
        ordering = ['-id']

此外,还有多个类包含Photo的外键。所有这些都很完美。

class SubstanceExistsLabel(PhotoLabelBase):
    """ Label indicating whether or not a substance is present in an image """

    #: photo being labeled
    photo = models.ForeignKey(Photo, related_name='substance_exists_labels')

    #: all points should have this substance
    substance = models.ForeignKey(SubstanceCategory)

class SubstancePointsLabel(ResultBase):
    """ List of points in an image that should have a certain substance """

    #: photo being labeled
    photo = models.ForeignKey(Photo, related_name='substance_points_labels')

    #: all points should have this substance
    substance = models.ForeignKey(SubstanceCategory)

class SubstancePoint(EmptyModelBase):
    """ A single point in a photo being considered for its substance """

    #: photo
    photo = models.ForeignKey(Photo, related_name='substance_points')

每个Photo实例都包含默认的唯一ID。

>>> Photo.objects.all()[0].id
    5960

现在我的问题。我尝试创建下面显示的另一个类Material,其中包含Photo的外键,就像SubstanceExistsLabelSubstancePointsLabelSubstancePoint一样做,以及其他多个类。

class Material(models.Model):
    #: photo being labeled
    original = models.ForeignKey(
        Photo, related_name='pictures',null=True,blank=True
    )

    name = models.CharField(max_length=128)
    asked = models.IntegerField(default=None)
    yes = models.IntegerField(default=None)

然而,每次我尝试迁移时,都会给我错误 django.db.utils.ProgrammingError: there is no unique constraint matching given keys for referenced table "photos_photo"。下面添加了完整的堆栈跟踪。

我仍然不明白这个错误意味着什么,或者已经对引用的表有一个唯一的约束,因为我能够用Photo的外键创建其他类。我怎样才能创建这个类?或者是否有一种解决办法来创建一对多关系,其中一个Photo有多个Material个实例?

完整堆栈跟踪

    Applying points.0014_auto_20180124_0528...Traceback (most recent call last):
  File "manage.py", line 13, in <module>
    execute_from_command_line(sys.argv)
  File "/data_nfs/opensurfaces2/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/data_nfs/opensurfaces2/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/data_nfs/opensurfaces2/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/data_nfs/opensurfaces2/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/data_nfs/opensurfaces2/venv/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 161, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/data_nfs/opensurfaces2/venv/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 68, in migrate
    self.apply_migration(migration, fake=fake)
  File "/data_nfs/opensurfaces2/venv/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 102, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/data_nfs/opensurfaces2/venv/local/lib/python2.7/site-packages/django/db/backends/schema.py", line 91, in __exit__
    self.execute(sql)
  File "/data_nfs/opensurfaces2/venv/local/lib/python2.7/site-packages/django/db/backends/schema.py", line 111, in execute
    cursor.execute(sql, params)
  File "/data_nfs/opensurfaces2/venv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/data_nfs/opensurfaces2/venv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/data_nfs/opensurfaces2/venv/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/data_nfs/opensurfaces2/venv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: there is no unique constraint matching given keys for referenced table "photos_photo"

0 个答案:

没有答案