[Django] #35047: Wrong sql query for migration

6 views
Skip to first unread message

Django

unread,
Dec 18, 2023, 6:23:47 AM12/18/23
to django-...@googlegroups.com
#35047: Wrong sql query for migration
--------------------------------------+----------------------------
Reporter: mgoldenbe | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 3.2
Severity: Normal | Keywords: sqlmigrate
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
--------------------------------------+----------------------------
I had the model:

```py
class UserInfo(models.Model):
user = models.ForeignKey(User, related_name='user_info',
on_delete=CASCADE)
picture = models.URLField(null = True, blank = True)
paying = models.BooleanField(default = False)
credits = models.DecimalField(max_digits=6, decimal_places=2)
expiry = models.DateTimeField()
usd_per_credit = models.FloatField(null = True, blank = True)
content_count = models.IntegerField(default=0)
suspended = models.BooleanField(default = False)
suspended_request = models.TextField(null = True, blank = True)
suspended_timestamp = models.DateTimeField(null = True, blank = True)

def __str__(self): return str(self.user)
```

Then I changed the `usd_per_credit` field to be
```
usd_per_credit = models.FloatField(default = 0.00)
```

After running `makemigrations` and `migrate`, I ran `sqlmigrate` and got
the following:
```sql
BEGIN;
--
-- Alter field usd_per_credit on userinfo
--
ALTER TABLE "accounts_userinfo" ALTER COLUMN "usd_per_credit" SET DEFAULT
0.0;
UPDATE "accounts_userinfo" SET "usd_per_credit" = 0.0 WHERE
"usd_per_credit" IS NULL;
ALTER TABLE "accounts_userinfo" ALTER COLUMN "usd_per_credit" SET NOT
NULL;
ALTER TABLE "accounts_userinfo" ALTER COLUMN "usd_per_credit" DROP
DEFAULT;
COMMIT;
```

The last line before COMMIT is wrong. It drops the default value which was
correctly set.

--
Ticket URL: <https://code.djangoproject.com/ticket/35047>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 18, 2023, 6:25:02 AM12/18/23
to django-...@googlegroups.com
#35047: Wrong sql query for migration
----------------------------+--------------------------------------

Reporter: mgoldenbe | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 3.2
Severity: Normal | Resolution:

Keywords: sqlmigrate | 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 mgoldenbe:

Old description:

New description:

I had the model:


{{{


class UserInfo(models.Model):
user = models.ForeignKey(User, related_name='user_info',
on_delete=CASCADE)
picture = models.URLField(null = True, blank = True)
paying = models.BooleanField(default = False)
credits = models.DecimalField(max_digits=6, decimal_places=2)
expiry = models.DateTimeField()
usd_per_credit = models.FloatField(null = True, blank = True)
content_count = models.IntegerField(default=0)
suspended = models.BooleanField(default = False)
suspended_request = models.TextField(null = True, blank = True)
suspended_timestamp = models.DateTimeField(null = True, blank = True)

def __str__(self): return str(self.user)
}}}


Then I changed the `usd_per_credit` field to be

{{{
usd_per_credit = models.FloatField(default = 0.00)
}}}


After running `makemigrations` and `migrate`, I ran `sqlmigrate` and got
the following:

{{{


BEGIN;
--
-- Alter field usd_per_credit on userinfo
--
ALTER TABLE "accounts_userinfo" ALTER COLUMN "usd_per_credit" SET DEFAULT
0.0;
UPDATE "accounts_userinfo" SET "usd_per_credit" = 0.0 WHERE
"usd_per_credit" IS NULL;
ALTER TABLE "accounts_userinfo" ALTER COLUMN "usd_per_credit" SET NOT
NULL;
ALTER TABLE "accounts_userinfo" ALTER COLUMN "usd_per_credit" DROP
DEFAULT;
COMMIT;
}}}

The last line before COMMIT is wrong. It drops the default value which was
correctly set.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/35047#comment:1>

Django

unread,
Dec 18, 2023, 6:25:26 AM12/18/23
to django-...@googlegroups.com
#35047: Wrong sql query for migration
----------------------------+--------------------------------------
Reporter: mgoldenbe | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 3.2
Severity: Normal | Resolution:
Keywords: sqlmigrate | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------------------------

Comment (by mgoldenbe):

Code format

--
Ticket URL: <https://code.djangoproject.com/ticket/35047#comment:2>

Django

unread,
Dec 18, 2023, 6:45:51 AM12/18/23
to django-...@googlegroups.com
#35047: Wrong sql query for migration
----------------------------+--------------------------------------
Reporter: mgoldenbe | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: 3.2
Severity: Normal | Resolution: invalid

Keywords: sqlmigrate | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => invalid


Comment:

`Field.default` doesn't set a permanent default at the database level. In
this case, it is set temporarily to populate existing rows. To use a
permanent database default, use
[https://docs.djangoproject.com/en/stable/ref/models/fields/#django.db.models.Field.db_default
Field.db_default] (new in Django 5.0).

--
Ticket URL: <https://code.djangoproject.com/ticket/35047#comment:3>

Reply all
Reply to author
Forward
0 new messages