Django 1.10 "Cannot force an update in save() with no primary key", using model with composite primary key (unique_together)

1,490 views
Skip to first unread message

in...@lucasvandijk.nl

unread,
Oct 29, 2016, 9:57:53 AM10/29/16
to Django users
Hi all,

I have a simple model whichs acts as custom many-to-many association table with additional data.

```
class NetworkLinkAssociation(models.Model):
    """
    We use our own many-to-many association table, because we want to keep
    track which links are added, and which links are deleted/disabled in
    comparison to the parent network.
    """

    network = models.ForeignKey('Network', related_name='links_assoc',
                                on_delete=models.CASCADE)
    link = models.ForeignKey('Link', related_name='networks_assoc',
                             on_delete=models.CASCADE)

    type = enum.EnumField(AssociationType, default=AssociationType.UNCHANGED,
                          null=True)

    class Meta:
        unique_together = ('network', 'link')

    @property
    def link_disabled(self):
        return self.type == AssociationType.REMOVED
```

When trying to simply create a new object django raises an error:

```
link_assoc = NetworkLinkAssociation()
link_assoc.link = link
link_assoc.network = network
link_assoc.save()
```

"ValueError: Cannot force an update in save() with no primary key.".

I'm 100% sure the `link` and `network` objects exist, so what else could the problem be? This used to work fine Django 1.9.

Possibly related bug on Github: https://github.com/jpwatts/django-positions/issues/49

Thanks in advance,
Lucas

Tim Graham

unread,
Oct 29, 2016, 4:50:19 PM10/29/16
to Django users
Try bisecting to find the commit in Django where the behavior changed:

https://docs.djangoproject.com/en/dev/internals/contributing/triaging-tickets/#bisecting-a-regression
Reply all
Reply to author
Forward
0 new messages