#25012: Migration doesn't seem to detect foreignKey type changes
-------------------------------------+------------------------------------
     Reporter:  Hedde van der Heide  |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Migrations           |                  Version:  master
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+------------------------------------

Comment (by Aleksi Häkli):

 I was able to neatly fix this with a raw SQL migration that changed the
 intermediary table column type.

 I had the following schema in an application called `documents`:


 {{{
 class Tag(models.Model):
     # converted this `name` field to primary_key
     # previously had the default `id` AutoField as primary_key field
     # PrimaryKeyField migration in for ManyToManyField is risky!
     name = models.CharField(max_length=32, primary_key=True)

 class Document(models.Model):
     tags = models.ManyToManyField(Tag, blank=True)
 }}}


 After changing the schema and running unit tests I discovered that I
 indeed to got a DatabaseError:

 {{{
 django.db.utils.DataError: invalid input syntax for integer: "Miss Amanda
 Goodwin"
 LINE 1: ..."tag_id") WHERE "documents_document_tags"."tag_id" = 'Miss
 Aman...
 }}}

 Migrating with the following helped:

 {{{
 ALTER TABLE documents_document_tags ALTER COLUMN tag_id TYPE varchar(32);
 }}}

 This of course had to be added as a migration file for sane functionality
 in tests and migration chain:

 {{{
 # -*- coding: utf-8 -*-
 # Migrations file example for fixing broken ManyToManyField migration from
 INTEGER to VARCHAR
 # Generated by Django 1.10.3 on 1970-01-01 00:00
 from __future__ import unicode_literals

 from django.db import migrations

 class Migration(migrations.Migration):
     dependencies = [
         ('documents', '0042_auto_19700101-0000'),
     ]

     operations = [
         migrations.RunSQL('ALTER TABLE documents_document_tags ALTER
 tag_id TYPE varchar(32);'),
     ]
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25012#comment:8>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/063.8a619cb5d240da0383b718f46c00f09a%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to