user2492364
user2492364

Reputation: 6713

django migrate has error: Specify a USING expression to perform the conversion

I change my model field from Charfiled() to GenericIPAddressField()

ip = models.GenericIPAddressField()

and use django 1.7 migrate

./manage.py makemigrations core
./manage.py migrate

But there is error:

return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column "ip" cannot be cast automatically to type inet
HINT:  Specify a USING expression to perform the conversion.

I try this,but not work:

ALTER TABLE core_message ALTER COLUMN ip TYPE inet USING (ip::inet);

error:

ERROR:  invalid input syntax for type inet: ""

What can I do now?
Please help me Thank you!

Upvotes: 8

Views: 3664

Answers (1)

panchicore
panchicore

Reputation: 11932

one quick fix will be to drop and create the field:

  1. delete the migration what is changing the field type.
  2. delete/comment the field ip
  3. make migrations
  4. get back/uncomment the field ip with the new field type
  5. make migrations
  6. migrate

I did this in production and restored the data with a previous csv backup and an python script of a few lines a code.

Upvotes: 9

Related Questions