Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#32435 closed Bug (invalid)

Bug in JsonField with null=False

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

Description

I recently updated my project's Django to version 3.1.5 and removed its jsonfield dependency to use the new JSONField released with Django 3.1. However i'm experiencing quite a strange bug.

I was experimenting creating a new JSONField with different options and here is what i found:

Suppose I have a migration like this

        migrations.AlterField(
            model_name='awsuserschedule',
            name='selected_ids',
            field=common.fields.JSONField(encoder=common.encoders.JSONEncoder, null=True),
        ),
        migrations.AlterField(
            model_name='awsuserschedule',
            name='tags',
            field=common.fields.JSONField(default=list, encoder=common.encoders.JSONEncoder),
        ),
        migrations.AlterField(
            model_name='userscheduleslog',
            name='instance_ids',
            field=common.fields.JSONField(default=list, encoder=common.encoders.JSONEncoder, null=False),
        ),

When i run sqlmigrate to analyze this migration i get:

-- Alter field selected_ids on awsuserschedule
--
ALTER TABLE `aws_user_schedules` MODIFY `selected_ids` json NULL;
--
-- Alter field tags on awsuserschedule
--
--
-- Alter field instance_ids on userscheduleslog
--

So you see that when null is not passed, or null=False, there is no sql query created at all. I'm using MySQL 5.7.22. Am i doing something wrong?

Thanks

Change History (4)

comment:1 by Ronaldo Campos, 3 years ago

Cc: Ronaldo Campos added
Summary: Bug in JsonField creation with null=FalseBug in JsonField with null=False

comment:2 by Mariusz Felisiak, 3 years ago

Component: UncategorizedDatabase layer (models, ORM)
Resolution: needsinfo
Status: newclosed

Thanks for this report, however there is not enough details to understand or reproduce your issue. What kind of JSONField did you use before? What was the models' state before this operations? Can you provide a sample project?

comment:3 by Ronaldo Campos, 3 years ago

I was using the jsonfield library. But, never mind i think i figured out what i was doing wrong. It was an issue on the previous migration states that was causing sqlmigrate to avoid doing anything. Feel free to close the bug report.

comment:4 by Mariusz Felisiak, 3 years ago

Resolution: needsinfoinvalid

Thanks for the feedback.

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