Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 518
user2492364 Avatar asked Nov 27 '14 05:11

user2492364


People also ask

How do I fix migration issues in Django?

you can either: temporarily remove your migration, execute python manage.py migrate, add again your migration and re-execute python manage.py migrate. Use this case if the migrations refer to different models and you want different migration files for each one of them.

What does the migrate command do in Django?

Migrations are Django's way of propagating changes you make to your models (adding a field, deleting a model, etc.) into your database schema. They're designed to be mostly automatic, but you'll need to know when to make migrations, when to run them, and the common problems you might run into.

What is difference between migrate and migration in Django?

So the difference between makemigrations and migrate is this: makemigrations auto generates migration files containing changes that need to be applied to the database, but doesn't actually change anyhting in your database. migrate will make the actual modifications to your database, based on the migration files.


1 Answers

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.

like image 133
panchicore Avatar answered Sep 17 '22 18:09

panchicore