Opened 5 months ago

Closed 5 months ago

#35509 closed Bug (invalid)

migration models.BooleanField(default=False) does not create a DEFAULT attribte on mysql table

Reported by: Daniel Ahern Owned by: nobody
Component: Database layer (models, ORM) Version: 5.0
Severity: Normal Keywords:
Cc: Daniel Ahern Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Python 3.12.0
Django 5.0.6
MariaDB/MySQL 8.0.30-0ubuntu0.22.04.1 (Ubuntu)

models.py:

from django.db import models

class TableBoolean(models.Model):
    field1 = models.BooleanField(default=False)

    class Meta:
        db_table = 'test_boolean'

Then I do:

python manage.py makemigrations v506

which makes:

# Generated by Django 5.0.6 on 2024-06-08 14:03

from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='TableBoolean',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('field1', models.BooleanField(default=False)),
            ],
            options={
                'db_table': 'test_boolean',
            },
        ),
    ]

Running the migration creates a table that does NOT have a default value:

mysql> describe test_boolean;
+--------+------------+------+-----+---------+----------------+
| Field  | Type       | Null | Key | Default | Extra          |
+--------+------------+------+-----+---------+----------------+
| id     | bigint     | NO   | PRI | NULL    | auto_increment |
| field1 | tinyint(1) | NO   |     | NULL    |                |
+--------+------------+------+-----+---------+----------------+

I am able to alter the table:

mysql> alter table test_boolean modify field1 boolean default 0;
Query OK, 0 rows affected (0.07 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> describe test_boolean;
+--------+------------+------+-----+---------+----------------+
| Field  | Type       | Null | Key | Default | Extra          |
+--------+------------+------+-----+---------+----------------+
| id     | bigint     | NO   | PRI | NULL    | auto_increment |
| field1 | tinyint(1) | YES  |     | 0       |                |
+--------+------------+------+-----+---------+----------------+

Change History (2)

comment:1 by Daniel Ahern, 5 months ago

Summary: migration models.BooleanField(default=False) does not create amigration models.BooleanField(default=False) does not create a DEFAULT attribte on mysql table
Type: UncategorizedBug

comment:2 by Tim Graham, 5 months ago

Resolution: invalid
Status: newclosed

As documented: The default value can also be set at the database level with Field.db_default.

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