1

I really need to know why in the world we cannot change the column types after we do the migrations to Postgres DB.

I already created my models by python manage.py makemigrations then do migrate. Everything looks fine and tables are created on the postgres DB.

class test_API(models.Model):
    IDnumber = models.IntegerField(null=False, blank=False)
    State = models.CharField(max_length = 256, null = True)
    Exlcludmethod = models.CharField(max_length=256, null=True)

class test_API_2(models.Model):
    Idnumber = models.Foreignkey(test_API, max_length = 256, blank=False, null = False)
    value = models.CharField(max_length=128, default="")
    last_updated = models.DateTimeField(auto_now=True)

Lets say we want to make change to IDnumber column from Integerfield to Charfield.

class test_API(models.Model):
        IDnumber = models.CharField(null=False, blank=False)
        State = models.CharField(max_length = 256, null = True)
        Exlcludmethod = models.CharField(max_length=256, null=True)

and run the python manage.py makemigrations again

return self.cursor.execute(sql) psycopg2.errors.DuplicateTable: relation "test_API" already exists

How can we modify/change column types and do migrations. What is the proper way of doing this ?

Using Django:Django==4.0.3 Python = 3.9.1 Posgresql = 2.9.3

Django migrations using RunPython to commit changes

django 1.7 migrate gets error "table already exists"

Alexander
  • 4,527
  • 5
  • 51
  • 98
  • you aren't just changing it from an integer to a char, you're changing it from a relation to another table to no relation. If you add a `related_name` to the Idnumber field you might have some luck. What values do you expect to be stored in the new IDnumber field? – AMG Apr 07 '22 at 23:26
  • @AMG I expect to store Abcd type strings in `IDnumber` field. – Alexander Apr 08 '22 at 15:03

1 Answers1

1

It seems like you want to drop the link to test_API in test_API_2. If you want to be able store 'abcd' type strings, and not try to keep anything from the relation previously created by the ForeignKey field, then I believe you need to do this in a two step process.

First step would be to remove (or comment) the IDNnumber field, make migrations, then add it back with a new type of field.

The IDNumber field was more than an integer field, it has an indexed relation to test_API, so you need to sever that first by removing the field, then create a new field with identical name.

AMG
  • 1,606
  • 1
  • 14
  • 25
  • thanks! wouldn't this remove the records in `IDnumber` ? – Alexander Apr 11 '22 at 15:41
  • yes. That was what I was asking when I said "what values to you expect to be stored" in IDNumber. Previously it was a link to `test_api` but if you are switching to a CharField what specific values do you want to be stored instead? Which field would they come from? You may be interested in: https://stackoverflow.com/a/64701570/4872140 – AMG Apr 11 '22 at 17:09
  • Thanks for the link. That is the solution that I was looking for! I think I'm going to store integers (1,2,3, ... ) or characters (a,bc,adssa...) so on in the charfield. – Alexander Apr 11 '22 at 17:54