Answering myself, AFAICT I need to create migrations which look like the 
following for every m2m intermediate table. The following assumes a "Book" 
model which has a ManyToMany relationship to a "Tag" model:


from django.db import migrations


class Migration(migrations.Migration):
    dependencies = [
        ("myapp", "0001_initial"),
    ]

    operations = [
        migrations.RunSQL(
            [
                'ALTER TABLE "myapp_book_tags" ALTER COLUMN "id" TYPE 
bigint USING "id"::bigint',
                'ALTER SEQUENCE "myapp_book_tags_id_seq" AS bigint',
            ],
            elidable=True,
        ),
    ]


Kind regards,
Jeremy


On Monday, May 16, 2022 at 8:52:06 AM UTC+2 Jeremy Lainé wrote:

> Hi Mike,
>
> Thanks for the reply. For future reference, it was pointed out to me that 
> I missed both an explicit note in the documentation [1] and an issue 
> tracking a potential change in the future [2].
>
> Do you have any suggestions as to the actual SQL migration which needs to 
> be applied for postgresql on each m2m table? Specifically, is it purely a 
> matter of altering the "id" column type, or does something need to be done 
> to the index?
>
> Cheers,
> Jeremy
>
> [1] https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field
> [2] https://code.djangoproject.com/ticket/32674
>
> On Sunday, May 15, 2022 at 10:26:18 AM UTC+2 Mike Dewhirst wrote:
>
>> On 14/05/2022 11:44 pm, Jeremy Lainé wrote:
>>
>> Hi!
>>
>> I'm currently looking at how to migrate all my models from AutoField to 
>> BigAutoField. For all the explicitly defined models the process seems 
>> pretty straightforward:
>>
>>    - change DEFAULT_AUTO_FIELD to BigAutoField 
>>    - generate migrations 
>>    - apply migrations 
>>
>> However, when I inspect the database schema, I see that the 
>> "intermediate" tables for many-to-many relations still have a primary key 
>> of type "integer" (on postgresql). This means I'm no closer to avoiding 
>> 32bit primary key exhaustion!
>>
>> Does anyone know how I can address this?
>>
>>
>> Not really. 
>>
>> In my case I specify models for all my m2m intermediate tables as a 
>> matter of course. It is how I think. In my schemas, most such tables 
>> typically carry extra data about the relationships they define - therefore 
>> I want models. The bigint migration obviously worked for me.
>>
>> Maybe you can use manage.py to write out your m2m models so you can 
>> adjust then migrate them. 
>>
>> settings.DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" should work 
>> for new intermediate tables.
>>
>> Another option is to manually adjust all your m2m PostgreSQL id fields to 
>> bigint.
>>
>> It is a lossless change and will never need to be reversed so it might be 
>> easier to bite that bullet and move on.
>>
>> YMMV
>>
>>
>> Many thanks in advance,
>> Jeremy
>>
>> -- 
>> 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 view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/de4fefa5-6dcf-4419-91f8-a6451cac781en%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/django-users/de4fefa5-6dcf-4419-91f8-a6451cac781en%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>>
>>
>> -- 
>> Signed email is an absolute defence against phishing. This email has
>> been signed with my private key. If you import my public key you can
>> automatically decrypt my signature and be sure it came from me. Just
>> ask and I'll send it to you. Your email software can handle signing.
>>
>>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/b29c0b6e-a643-4368-b862-f6ccfde4c9fan%40googlegroups.com.

Reply via email to