'Can we update a value after it has been referenced as a foreign key in django?

I have two tables:

class LocationType(models.Model):
    location_type = models.CharField(max_length=120, unique=True)
    comment = models.TextField(null=True, blank=True)

    def __str__(self):
        return self.location_type 

class Location(models.Model):
    location = models.CharField(max_length=100, unique=True)
    location_type = models.ForeignKey(LocationType, to_field="location_type", on_delete=models.CASCADE) 
    comment = models.TextField(null=True, blank=True) 

    def __str__(self):
        return self.location 


and I have created a location_type 'PLANT' and then referenced it while creating a location "GUJRAT".

location_type1 = LocationType.objects.create(location_type="PLANT")
location1 = Location.objects.create(location="GUJRAT", location_type=location_type1)

Now after creating these two entries I want to change location_types value from "PLANT" to "FACTORIES".

location_type1.location_type = "FACTORIES"
location_type1.save() 

But I am getting below error:

Traceback (most recent call last):
  File "/home/jayantseth/Projects/ip_manager/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
  File "/home/jayantseth/Projects/ip_manager/venv/lib/python3.8/site-packages/django/db/backends/sqlite3/base.py", line 477, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: FOREIGN KEY constraint failed

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/jayantseth/Projects/ip_manager/IpManagerBackend/ipmanager/models.py", line 58, in save
    super(LocationType, self).save(*args, **kwargs)
  File "/home/jayantseth/Projects/ip_manager/venv/lib/python3.8/site-packages/django/db/models/base.py", line 806, in save
    self.save_base(
  File "/home/jayantseth/Projects/ip_manager/venv/lib/python3.8/site-packages/django/db/models/base.py", line 857, in save_base
    updated = self._save_table(
  File "/home/jayantseth/Projects/ip_manager/venv/lib/python3.8/site-packages/django/db/models/base.py", line 970, in _save_table
    updated = self._do_update(
  File "/home/jayantseth/Projects/ip_manager/venv/lib/python3.8/site-packages/django/db/models/base.py", line 1034, in _do_update
    return filtered._update(values) > 0
  File "/home/jayantseth/Projects/ip_manager/venv/lib/python3.8/site-packages/django/db/models/query.py", line 885, in _update
    return query.get_compiler(self.db).execute_sql(CURSOR)
  File "/home/jayantseth/Projects/ip_manager/venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1783, in execute_sql
    cursor = super().execute_sql(result_type)
  File "/home/jayantseth/Projects/ip_manager/venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1361, in execute_sql
    cursor.execute(sql, params)
  File "/home/jayantseth/Projects/ip_manager/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 103, in execute
    return super().execute(sql, params)
  File "/home/jayantseth/Projects/ip_manager/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(
  File "/home/jayantseth/Projects/ip_manager/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/jayantseth/Projects/ip_manager/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
  File "/home/jayantseth/Projects/ip_manager/venv/lib/python3.8/site-packages/django/db/utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/jayantseth/Projects/ip_manager/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
  File "/home/jayantseth/Projects/ip_manager/venv/lib/python3.8/site-packages/django/db/backends/sqlite3/base.py", line 477, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: FOREIGN KEY constraint failed

Is it possible to do what I am trying here or do I need to handle it at application level ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source