#27456 closed Bug (worksforme)

Changing the unique parameter from True to False has no effect on MySQL

Reported by: Vadim Owned by: nobody
Component: Migrations Version: 1.8
Severity: Normal Keywords: mysql
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Vadim)

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)
  1. 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)),
            ],
        ),
    ] 
  1. 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)
  1. makemigrations && migrate
  2. 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

Change History (5)

comment:1 Changed 23 months ago by Vadim

Summary: Changing the unique paramater from True to False has no effect on MySQLChanging the unique parameter from True to False has no effect on MySQL

comment:2 Changed 23 months ago by Shai Berger

According to the description, after creating the second migration you only ran the sqlmigrate command, not migrate. Is the error in your process or your ticket description?

comment:3 Changed 23 months ago by Vadim

Description: modified (diff)

comment:4 in reply to:  2 Changed 23 months ago by Vadim

Replying to Shai Berger:

According to the description, after creating the second migration you only ran the sqlmigrate command, not migrate. Is the error in your process or your ticket description?

It was an error in my description. I updated it. Thank you!

comment:5 Changed 23 months ago by Tim Graham

Resolution: worksforme
Status: newclosed

I can't reproduce a problem. There's also a test in schema/tests.py that seems to cover this case. Please provide a test case or point out where Django is at fault.

Note: See TracTickets for help on using tickets.
Back to Top