Thursday, July 23, 2015

Re: Migrate a many-to-many field to an explicit intermediate ("through") model?

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

iQIcBAEBCAAGBQJVsVBwAAoJEC0ft5FqUuEhsOUP/30pbbcap8w7PdSXnXjt/FAF
jsrbTOFmBE7xuoXbQDj6kPcvyyTkyvylt9C92F4RvFziopcH45BnIImiTHsMtBYN
R24a0mBb2q6a+Ps49zDniXrCQrmeuchwhngg0AW5s3syc9SPwb6Flrmz+NIpkuKJ
0maPifwFreFknhmku4LMS7hdaRyYOp5s+8AeLZKzfUSNPZEeT7ToyobOXzycgiC4
fFaguOplLBeOP5VCh0dhf6MSoYoAO7sD4aFpwuA8hU1cTUDIdPAQzz7mShFKjemI
q5Ulyi6yUjbE56b+hvj/gDPo3uUSxlWfwXDa/RxVTFsi2fEixeF565LCVMiNC9wj
GE4qeC/XmthHq6CAp2weVph1uOS2eOHDcR0umdx0Ubh8ClrzwrPmvfWP1Zi2TkQD
ikwfusQhHOvjJ9PelNtIvETiWTtjdZZ9P/oTj+jfsXVbz3tgPefrvIN+wtcQ6Dz5
2ffH+xnZBU2mUNC+C4i+uXMyyelMupceD98BWH6DXLCYmu9gdm22+qAyg8o5s9vc
i1XnTtBJb1sGNGiFbwynb6t9jQUxxlN65OQr9M7nwp2OaQmvI3o9S6XbSY8lT5Jf
MckzHt2wfeAcKZ3PMlthaAj+HVN+L37U4z/RQ3El9G28r8wcmZ4mwZN8OmDzKTM7
Lb0VioQU2x04tBtTrhkM
=v3ze
-----END PGP SIGNATURE-----
Hi Gergely,

On 07/23/2015 02:27 PM, Gergely Polonkai wrote:
> Yes, you are right, my attempt is not the solution to your problem; it
> seems that this m2m field really cannot be modified like this. With some
> slight modifications, though, it may be.
>
> 1) create the through table
> 2) migrate data with RunPython — if you want to be portable, stay with
> RunPython instead of RunSQL

Yes, this is true. Don't use RunSQL if you want your code to be portable
between database backends. (For most of my non-reusable-apps code, I
take great advantage of Postgres-specific features and don't care at all
about cross-database portability.)

> 3) delete the ManyToManyField
> 4) recreate the ManyToManyField with the through option
>
> All this can (and I think, should) go into the same migration file.

Hmm. I'm almost sure I've had problems in the past with trying to do
schema alterations and data migration in the same migration file (thus
same transaction), but with a simple test just now it seems to work OK
on Postgres. I'll have to start trying to combine migrations like this
more often and see how it goes.

> Meanwhile it might worth a question to the devs (or a more thorough
> search on the topic) to understand why you can't switch from a simple
> m2m field to one with a through option.

Just because it's a bit complex to implement, and nobody has implemented
it yet.

Carl

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/55B15070.8000707%40oddbird.net.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment