Trying to replace a char field with a forgein key feild

27 views
Skip to first unread message

matts...@overstock.com

unread,
Aug 27, 2018, 12:46:10 PM8/27/18
to Django users
So when we started the database, it was good enough to have a char field with choices. But as time progressed I need to convert that field to a foreign key field. The follow I'm trying to take is such:

  1. rename <fieldname> to tmp_<fieldname>
  2. create a new field with <fieldname>
  3. have a RunPython fill the data for <fieldname>
  4. delete field tmp_<fieldname>
I can get steps 1 through 3 to work with out issue. But once I remove the old field from the code, the migration breaks because the field no longer exists (which makes sense). I'm curious if there is a way to work around this, because my googling is coming up empty.

Codewise IO

unread,
Aug 28, 2018, 7:35:47 AM8/28/18
to Django users
You are going to want to create an empty migrations file and edit it to make your data transformations. See this article.

https://simpleisbetterthancomplex.com/tutorial/2017/09/26/how-to-create-django-data-migrations.html

Matthew Stroud

unread,
Aug 28, 2018, 12:56:29 PM8/28/18
to django...@googlegroups.com
Thanks for the reply.

This is a perfect coverage of steps 1 through 3 in what I'm doing, but it will break when I do step 4. I basically need to extend the model temporarily so the code doesn't break (I just thought of this so I'll try to implement it).

Thanks,
Matthew Stroud

On 8/28/18, 5:35 AM, "django...@googlegroups.com on behalf of Codewise IO" <django...@googlegroups.com on behalf of codew...@gmail.com> wrote:

You are going to want to create an empty migrations file and edit it to make your data transformations. See this article.

https://simpleisbetterthancomplex.com/tutorial/2017/09/26/how-to-create-django-data-migrations.html

--
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/986ed380-38dc-4188-ae0e-d97a9a0086b7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



________________________________

CONFIDENTIALITY NOTICE: This message is intended only for the use and review of the individual or entity to which it is addressed and may contain information that is privileged and confidential. If the reader of this message is not the intended recipient, or the employee or agent responsible for delivering the message solely to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify sender immediately by telephone or return email. Thank you.

Michal Petrucha

unread,
Aug 29, 2018, 7:32:52 AM8/29/18
to Django users
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

On Mon, Aug 27, 2018 at 09:37:03AM -0700, mattstroud via Django users wrote:
> So when we started the database, it was good enough to have a char field
> with choices. But as time progressed I need to convert that field to a
> foreign key field. The follow I'm trying to take is such:
>
>
> 1. rename <fieldname> to tmp_<fieldname>
> 2. create a new field with <fieldname>
> 3. have a RunPython fill the data for <fieldname>
> 4. delete field tmp_<fieldname>
>
> I can get steps 1 through 3 to work with out issue. But once I remove the
> old field from the code, the migration breaks because the field no longer
> exists (which makes sense). I'm curious if there is a way to work around
> this, because my googling is coming up empty.

I'm not following – why would the migration break? What part of it
would break, and how? None of what you wrote suggests that this should
in any way fail, as long as operations 1, 2, and 4 are performed with
AlterField, AddField, and RemoveField, and you use model classes
passed into RunPython through the ``apps`` argument.

Could you perhaps provide some more details about the failure that
you're experiencing?

Michal
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQIcBAEBCgAGBQJbhoRCAAoJEHA7T/IPM/klbKAQAJrCF9WNMOOsaWc3V/5PzNN3
quKafkPijxOkSl3AlLcPE6Os/2gPAc7rQZ+jKIorkXP6/RTh1U2DgGzYhLxeEzJL
tcS8cTtfsRx50En7Rbq9MnvQd/3G/xbEPaPzIp7moB1R0z7DY6uJoY39hpeJltd4
D2OVBwtryKs3r98B2PsQuwA5m7i8qy14LzosK1TQUGeUzEwINzaGQdt07aI24hpE
XJtfkUMXk7v8t3OuEPaSNCabfK0bGhV8WYRFj1JKUONrKhTl7RCXXBpmDahH2M5P
zInUKwChxa6zOwCpZ8tgoj2NSB0xlWSBzCZPV6LXiUvy7bPIJ9N64MBbIP9ntsEL
5ozGwE8oSmVpdDAlGY8IdwnJdgJzemRokD1z2PKo4zJViDrDxdOB4dVmrJ/FjrH6
cYe022N8iHHVx9aAtMXyuitfRzcNukpUTiyXPDji3XeUAxjmR3/sezWXiCA0iDua
vc3mytzQldPw2t7E9q0a/Q4QvFAh42ALzpZzZwdVmx81j3CGbbF6GiWuYiL/xYUc
bTt9RsEAFiNiDXWXiljf94ZwDQaAd+sHCwHP+zleHHKQ4n2iAZrb+Cy4vq2uNpGi
hZRAmIgOkqcN4kaD4wnImYigydSVgJ1AtL+MTcIgm8U06uSTicjRs67+RWvmvS6G
bpaFyP2KsdzU58s0sTTA
=LaQQ
-----END PGP SIGNATURE-----

Matthew Stroud

unread,
Aug 29, 2018, 12:16:29 PM8/29/18
to django...@googlegroups.com
Thanks for the reply.

Sadly in the process of me messing around with, I somehow fixed it. I'm not sure how, but when I get a chance I'll look through my changes and see what did it.

In the meantime, I'm going to claim user error until I can figure it out.

Thanks,
Matthew Stroud
--
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/20180829113220.GV1181%40koniiiik.org.
Reply all
Reply to author
Forward
0 new messages