Discussion:
Migration Issue in Django 1.9
china
2018-11-12 16:39:13 UTC
Permalink
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
--
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.
Andréas Kühne
2018-11-12 17:25:32 UTC
Permalink
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
Post by china
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
--
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
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
<https://groups.google.com/d/msgid/django-users/20ca05ad-6486-489c-91fc-62adf615dee3%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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/CAK4qSCcpCfsWvuTn0OZWD8tGY5_bhfeUVMr-SkJEB1nwfpWLFQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
china
2018-11-12 18:58:32 UTC
Permalink
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
Post by Andréas Kühne
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.
1. Rename the current field.
2. Add the new foreign key field
3. Migrate the data via a custom migration.
Regards,
Andréas
Post by china
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
--
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
<javascript:>.
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
<https://groups.google.com/d/msgid/django-users/20ca05ad-6486-489c-91fc-62adf615dee3%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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/7414b861-7c76-4c23-8ede-c358a24c76fa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Andréas Kühne
2018-11-12 19:06:29 UTC
Permalink
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
Post by china
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
Post by Andréas Kühne
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.
1. Rename the current field.
2. Add the new foreign key field
3. Migrate the data via a custom migration.
Regards,
Andréas
Post by china
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
--
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
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
<https://groups.google.com/d/msgid/django-users/20ca05ad-6486-489c-91fc-62adf615dee3%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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
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/7414b861-7c76-4c23-8ede-c358a24c76fa%40googlegroups.com
<https://groups.google.com/d/msgid/django-users/7414b861-7c76-4c23-8ede-c358a24c76fa%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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/CAK4qSCf8_oDQPXBTMnPAQ3A3MMghSEh%3DdbeXt%2Bk_zPx%2BnkzNCQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
china
2018-11-12 19:54:03 UTC
Permalink
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
Post by Andréas Kühne
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
Post by china
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
Post by Andréas Kühne
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.
1. Rename the current field.
2. Add the new foreign key field
3. Migrate the data via a custom migration.
Regards,
Andréas
Post by china
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
--
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
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
<https://groups.google.com/d/msgid/django-users/20ca05ad-6486-489c-91fc-62adf615dee3%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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
<javascript:>.
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/7414b861-7c76-4c23-8ede-c358a24c76fa%40googlegroups.com
<https://groups.google.com/d/msgid/django-users/7414b861-7c76-4c23-8ede-c358a24c76fa%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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/6365fe98-6b75-427d-b7bc-cc0feb37d887%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Loading...