Error "Can not update Identity column 'order'"

Asked

Viewed 56 times

0

Due to an old need of a project, the developer of the time created a field in the table that is not a primary key but is auto incrementable, however, Django returns an error for not being able to update this field. Django’s Autofield type is intended for primary key, the same cannot be used in this case, but I don’t know if there is any way to map the table correctly.

Example of a model representing the video table.

class Video(models.Model):
    id = UUIDField(db_column='ID', default=uuid.uuid4, auto=True)
    titulo = models.CharField(null=False, max_length=200, db_column='DsVideo')
    order = models.IntegerField(db_column='order')

The "order" field is that this in the database is as auto incrementable, but when saving, the error Can not update identity column 'order' is returned.

  • @Charleschessman now looks good, because when answers come, they can help not only you, but other users of the site who don’t understand English.

  • 1

    Charles, welcome to [en.so]! Thank you for translating your question. Sorry for any friction you may have felt, but each community has its rules and the purpose of this is to provide quality content for the Portuguese speaking public. Many here understand English, but similarly there would also be no answer about puzzles on the site about code review, even if the same users are on both sites. This is essential because the goal of the network is not only to solve specific problems, but to provide a relevant knowledge base for all. Have fun!

  • Good morning, the problem is more with bank than with Django, which bank is using?

1 answer

0

That question in Soen the author has found a temporary solution, which is to remove the problematic fields of query insertion/updating. Adapted to your code:

class Video(models.Model):
    id = UUIDField(db_column='ID', default=uuid.uuid4, auto=True)
    titulo = models.CharField(null=False, max_length=200, db_column='DsVideo')
    order = models.IntegerField(db_column='order')

    def save(self, *args, **kwargs):
        if self.order is not None:
            self._meta.local_fields = [f for f in self._meta.local_fields if f.name != 'order']

As the DBMS itself is responsible for updating this field, the fact that Django ignores it in writing (but still gives you the option to access it for reading) should not be a problem. Note: the question is old (2011), I can not guarantee that the technique still works in the current versions of Django.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.