Migration Issue in Django 1.9

26 views
Skip to first unread message

china

unread,
Nov 12, 2018, 11:39:13 AM11/12/18
to Django users
Hi Experts,

I am trying to change the charfield to ForeignKey and I am running my code I am getting the following error if do python manage.py migrate

django.db.utils.DataError: invalid input syntax for integer: "Jeff"


Here is my code


BEFORE

------


reports_to = models.CharField(max_length=128, blank=True, null=True)


in_country_manager
= models.CharField(max_length=128, blank=True, null=True)

After
------

reports_to = models.ForeignKey("self", null=True, blank=True, related_name="employee_reports_to")


Could any please help with this issue.


Thanks,

Karthik

Andréas Kühne

unread,
Nov 12, 2018, 12:26:23 PM11/12/18
to django...@googlegroups.com
Hi,

You are changing a CharField into a ForeignKey field with data present. So when the migration tries to run, the reports_to field contains information with a string in it - the database server then tries to set it to a ForeignKey field, which is an integer field per default. This data migration doesn't work and that is what the error is saying.

This can't be migrated automatically without writing your own migrations and even then it won't be easy,.

If you don't care about the current value of the reports_to field, you can do this in 2 migrations - one to remove the field and another to add the foreign key field. Or you can just add a drop column for the old reports to field in the same migration.

If you do care, I would probably do something like this:
1. Rename the current field.
2. Add the new foreign key field
3. Migrate the data via a custom migration.

Regards,

Andréas


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/20ca05ad-6486-489c-91fc-62adf615dee3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

china

unread,
Nov 12, 2018, 1:58:32 PM11/12/18
to Django users
Hi Andreas,

Thank you for you quick response

Is it possible if remove my migration files and rollback my migration to three  migrations back..?

Thanks,
Karthik

Andréas Kühne

unread,
Nov 12, 2018, 2:07:18 PM11/12/18
to django...@googlegroups.com
Hi,

Yes you can roll back your migrations (I think).... But you weren't able to get by that migration and it's only that migration that you need to handle. The other migrations haven't yet been migrated and you can just delete them from your disk in that case?

Regards,

Andréas


china

unread,
Nov 12, 2018, 2:54:03 PM11/12/18
to Django users
Hi Andreas,

I have fixed the issue. Here is how i achieved....

  1. I removed the field reports_to and added a field report_new(with foreign key)
        -  reports_to = models.CharField(max_length=128, blank=True, null=True)---(Removed this line)
       
+ report_new = models.ForeignKey("self", null=True, blank=True, related_name="employee_reports_to")



        2. Altered the field name reports_new to reports_to
         changed this
         
report_new = models.ForeignKey("self", null=True, blank=True, related_name="employee_reports_to")
 
         to 
report_new = models.ForeignKey("self", null=True, blank=True, related_name="employee_reports_to")

Thank you very much for your response it helped me a lot.

Thanks,
Karthik
Reply all
Reply to author
Forward
0 new messages