#27456: Changing the unique parameter from True to False has no effect on MySQL
----------------------------+--------------------------------------
     Reporter:  Vadim       |                    Owner:  nobody
         Type:  Bug         |                   Status:  new
    Component:  Migrations  |                  Version:  1.8
     Severity:  Normal      |               Resolution:
     Keywords:  mysql       |             Triage Stage:  Unreviewed
    Has patch:  0           |      Needs documentation:  0
  Needs tests:  0           |  Patch needs improvement:  0
Easy pickings:  0           |                    UI/UX:  0
----------------------------+--------------------------------------
Description changed by Vadim:

Old description:

> Steps to reproduce:
> 1. Create a model:
>
> {{{
> from django.db import models
>
> class TestModel(models.Model):
>     name = models.CharField(max_length=100, unique=True)
>     surname = models.CharField(max_length=50)
> }}}
> 2. makemigrations && migrate
>
> {{{
> $ ./manage.py sqlmigrate testapp 0001_initial
> BEGIN;
> CREATE TABLE `testapp_testmodel` (`id` integer AUTO_INCREMENT NOT NULL
> PRIMARY KEY, `name` varchar(100) NOT NULL UNIQUE, `surname` varchar(50)
> NOT NULL);
>
> COMMIT;
> }}}
>
> {{{
> $ cat testapp/migrations/0001_initial.py
> # -*- coding: utf-8 -*-
> from __future__ import unicode_literals
>
> from django.db import migrations, models
>

> class Migration(migrations.Migration):
>
>     dependencies = [
>     ]
>
>     operations = [
>         migrations.CreateModel(
>             name='TestModel',
>             fields=[
>                 ('id', models.AutoField(verbose_name='ID',
> serialize=False, auto_created=True, primary_key=True)),
>                 ('name', models.CharField(unique=True, max_length=100)),
>                 ('surname', models.CharField(max_length=50)),
>             ],
>         ),
>     ]
> }}}
>
> 3. Set unique to False, makemigrations and migrate
>
> {{{
> from django.db import models
>
> class TestModel(models.Model):
>     name = models.CharField(max_length=100, unique=False)
>     surname = models.CharField(max_length=50)
> }}}
>
> {{{
> $ cat testapp/migrations/0002_auto_20161106_2004.py
> # -*- coding: utf-8 -*-
> from __future__ import unicode_literals
>
> from django.db import migrations, models
>

> class Migration(migrations.Migration):
>
>     dependencies = [
>         ('testapp', '0001_initial'),
>     ]
>
>     operations = [
>         migrations.AlterField(
>             model_name='testmodel',
>             name='name',
>             field=models.CharField(max_length=100),
>         ),
>     ]
> }}}
>
> {{{
> $ ./manage.py sqlmigrate testapp 0002_auto_20161106_2004
> }}}
>

> {{{
> mysql> show create table testapp_testmodel;
> +-------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
> | Table             | Create Table
> |
> +-------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
> | testapp_testmodel | CREATE TABLE `testapp_testmodel` (
>   `id` int(11) NOT NULL AUTO_INCREMENT,
>   `name` varchar(100) NOT NULL,
>   `surname` varchar(50) NOT NULL,
>   PRIMARY KEY (`id`),
>   UNIQUE KEY `name` (`name`)
> ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
> +-------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
> 1 row in set (0,00 sec)
> }}}
>
> Notes:
> It works well on SQLite
> This bug also exists in 1.10

New description:

 Steps to reproduce:
 1. Create a model:

 {{{
 from django.db import models

 class TestModel(models.Model):
     name = models.CharField(max_length=100, unique=True)
     surname = models.CharField(max_length=50)
 }}}
 2. makemigrations && migrate

 {{{
 $ ./manage.py sqlmigrate testapp 0001_initial
 BEGIN;
 CREATE TABLE `testapp_testmodel` (`id` integer AUTO_INCREMENT NOT NULL
 PRIMARY KEY, `name` varchar(100) NOT NULL UNIQUE, `surname` varchar(50)
 NOT NULL);

 COMMIT;
 }}}

 {{{
 $ cat testapp/migrations/0001_initial.py
 # -*- coding: utf-8 -*-
 from __future__ import unicode_literals

 from django.db import migrations, models


 class Migration(migrations.Migration):

     dependencies = [
     ]

     operations = [
         migrations.CreateModel(
             name='TestModel',
             fields=[
                 ('id', models.AutoField(verbose_name='ID',
 serialize=False, auto_created=True, primary_key=True)),
                 ('name', models.CharField(unique=True, max_length=100)),
                 ('surname', models.CharField(max_length=50)),
             ],
         ),
     ]
 }}}

 3. Set unique to False

 {{{
 from django.db import models

 class TestModel(models.Model):
     name = models.CharField(max_length=100, unique=False)
     surname = models.CharField(max_length=50)
 }}}

 4. makemigrations && migrate
 5. Result is:

 {{{
 $ cat testapp/migrations/0002_auto_20161106_2004.py
 # -*- coding: utf-8 -*-
 from __future__ import unicode_literals

 from django.db import migrations, models


 class Migration(migrations.Migration):

     dependencies = [
         ('testapp', '0001_initial'),
     ]

     operations = [
         migrations.AlterField(
             model_name='testmodel',
             name='name',
             field=models.CharField(max_length=100),
         ),
     ]
 }}}

 {{{
 $ ./manage.py sqlmigrate testapp 0002_auto_20161106_2004
 }}}


 {{{
 mysql> show create table testapp_testmodel;
 
+-------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
 | Table             | Create Table
 |
 
+-------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
 | testapp_testmodel | CREATE TABLE `testapp_testmodel` (
   `id` int(11) NOT NULL AUTO_INCREMENT,
   `name` varchar(100) NOT NULL,
   `surname` varchar(50) NOT NULL,
   PRIMARY KEY (`id`),
   UNIQUE KEY `name` (`name`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
 
+-------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
 1 row in set (0,00 sec)
 }}}

 Notes:
 It works well on SQLite
 This bug also exists in 1.10

--

--
Ticket URL: <https://code.djangoproject.com/ticket/27456#comment:3>
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/064.faaa27d3cfd19716bdc0d81bff8cf41e%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to