#32425: MySQL Schema is different about the same class definitions. (depends on
create table vs alter table)
--------------------------------------+--------------------------
Reporter: Jordan Bae | Owner: nobody
Type: Bug | Status: assigned
Component: Migrations | Version: master
Severity: Normal | Keywords: mysql
Triage Stage: Unreviewed | Has patch: 1
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
--------------------------------------+--------------------------
Hi, My name is Jordan. When I delete the column, I found some picky points
in Django.
MySQL Schema is different about the same class definitions.
MySQL Schemas are different about the nullable column default value.
For example,
1) when it was made by 'create table'
{{{
class PhoneBook(models.Model):
name = models.CharField(max_length=32, null=True, blank=True,
default='jordan')
phone_number = models.CharField(max_length=32, null=True, blank=True)
}}}
The above code creates the migrations file as shown below.
{{{
operations = [
migrations.CreateModel(
name='PhoneBook',
fields=[
('id', models.AutoField(auto_created=True,
primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(blank=True, default='jordan',
max_length=32, null=True)),
('phone_number', models.CharField(blank=True,
max_length=32, null=True)),
],
),
]
}}}
When Migrate, SQL is executed as shown below.
{{{
CREATE TABLE `main_phonebook` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`name` varchar(32) NULL,
`phone_number` varchar(32) NULL
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
}}}
2) when it was made by 'alter table'
For the situation where the ‘alter table’ is applied, let's define the
model class first as below and then add the name column.
{{{
class PhoneBook2(models.Model):
phone_number = models.CharField(max_length=32, null=True, blank=True)
}}}
and I added 'name' column.
{{{
class PhoneBook2(models.Model):
name = models.CharField(max_length=32, null=True, blank=True,
default='jordan')
phone_number = models.CharField(max_length=32, null=True, blank=True)
}}}
{{{
operations = [
migrations.AddField(
model_name='phonebook2',
name='name',
field=models.CharField(blank=True, default='jordan',
max_length=32, null=True),
),
]
}}}
this operations make this SQL.
{{{
ALTER TABLE `main_phonebook2` ADD COLUMN `name` varchar(32) DEFAULT %s
NULL ['jordan']
ALTER TABLE `main_phonebook2` ALTER COLUMN `name` DROP DEFAULT []
}}}
and table schema is like below.
{{{
CREATE TABLE `main_phonebook2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`phone_number` varchar(32) DEFAULT NULL,
`name` varchar(32),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
}}}
== **We can see that the same code creates different schemas.**
--
Ticket URL: <https://code.djangoproject.com/ticket/32425>
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 view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/052.9333a94f06d012779f2f2bae6dd9b763%40djangoproject.com.