[Django] #34333: Django migrations adds constraint before adding field

80 views
Skip to first unread message

Django

unread,
Feb 12, 2023, 1:59:02 PM2/12/23
to django-...@googlegroups.com
#34333: Django migrations adds constraint before adding field
-------------------------------------+-------------------------------------
Reporter: Raphael | Owner: nobody
Beekmann |
Type: Bug | Status: new
Component: | Version: 4.1
Migrations | Keywords: migration,
Severity: Normal | constraint, field
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Hello,

I have a model, already created through previous migrations, and in a new
migration I added a new field with a UniqueConstraint.
The model looks like this (the new field is 'type') :


{{{
class Model(models.Model):
name = models.CharField()
date = models.DateField()
type = models.ForeignKey(OtherModel)

class Meta:
constraints = (
models.UniqueConstraint(fields=('date', 'type'),
name='unique_date_for_type'),
)
}}}

When I run the makemigrations script it adds first the constraint and then
the new field. That occurs an error when executing the migration :

{{{
django.core.exceptions.FieldDoesNotExist: DailyTask has no field named
'type'
}}}

I have to manually move the adds of the constraint before the adds of the
new field in the migration file to make it work.

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

Django

unread,
Feb 13, 2023, 3:02:38 AM2/13/23
to django-...@googlegroups.com
#34333: Django migrations adds constraint before adding field
-------------------------------------+-------------------------------------
Reporter: Raphael Beekmann | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: 4.1
Severity: Normal | Resolution: needsinfo
Keywords: migration, | Triage Stage:
constraint, field | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

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


Comment:

Thanks for the report, however I cannot reproduce this issue. For me,
`makemigrations` generates operations in the correct order. Please reopen
the ticket if you can debug your issue and provide a small project that
reproduces it.

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

Django

unread,
Feb 13, 2023, 4:10:01 PM2/13/23
to django-...@googlegroups.com
#34333: Django migrations adds constraint before adding field
-------------------------------------+-------------------------------------
Reporter: Raphael Beekmann | Owner: nobody
Type: Bug | Status: new

Component: Migrations | Version: 4.1
Severity: Normal | Resolution:
Keywords: migration, | Triage Stage:
constraint, field | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Raphael Beekmann):

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


Old description:

> Hello,
>
> I have a model, already created through previous migrations, and in a new
> migration I added a new field with a UniqueConstraint.
> The model looks like this (the new field is 'type') :
>

> {{{
> class Model(models.Model):
> name = models.CharField()
> date = models.DateField()
> type = models.ForeignKey(OtherModel)
>
> class Meta:
> constraints = (
> models.UniqueConstraint(fields=('date', 'type'),
> name='unique_date_for_type'),
> )
> }}}
>
> When I run the makemigrations script it adds first the constraint and
> then the new field. That occurs an error when executing the migration :
>
> {{{
> django.core.exceptions.FieldDoesNotExist: DailyTask has no field named
> 'type'
> }}}
>
> I have to manually move the adds of the constraint before the adds of the
> new field in the migration file to make it work.

New description:

Hello,

I have a model, already created through previous migrations, and in a new

migration I added a new field with an UniqueConstraint. The migrations
script try to create the constraint first and then the new field,
resulting an error :

{{{
django.core.exceptions.FieldDoesNotExist: NewModel has no field named
'category'
}}}


To reproduce the bug :

1. Create a project with two models linked together with a One-to-Many
relation and an unique constraint :

{{{
class Type(models.Model):
name = models.CharField(max_length=10)


class Model(models.Model):
name = models.CharField(max_length=10)
type = models.ForeignKey(Type, on_delete=models.SET_NULL, null=True)
date = models.DateField(auto_now=True)

class Meta:
constraints = (
models.UniqueConstraint(fields=('date', 'type'),

name='unique_type_for_date'),
)
}}}

2. Create a migration file with `manage.py makemigrations`

3. Add a new model with another One-to-Many relation and unique
constraint. The models looks like this :


{{{
class Type(models.Model):
name = models.CharField(max_length=10)


class Category(models.Model):
name = models.CharField(max_length=10)


class Model(models.Model):
name = models.CharField(max_length=10)
type = models.ForeignKey(Type, on_delete=models.SET_NULL, null=True)
category = models.ForeignKey(Category, on_delete=models.SET_NULL,
null=True)
date = models.DateField(auto_now=True)

class Meta:
constraints = (
models.UniqueConstraint(fields=('date', 'type'),

name='unique_type_for_date'),
models.UniqueConstraint(fields=('date', 'category'),
name='unique_category_for_date'),
)
}}}


4. Create a new migration file. The order of the migration's steps are
incorrect and the migration crash :


{{{
class Migration(migrations.Migration):

dependencies = [
('app', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='Category',
fields=[
('id', models.BigAutoField(auto_created=True,
primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=10)),
],
),
migrations.AddConstraint(
model_name='model',
constraint=models.UniqueConstraint(fields=('date',
'category'), name='unique_category_for_date'),
),
migrations.AddField(
model_name='model',
name='category',
field=models.ForeignKey(null=True,
on_delete=django.db.models.deletion.SET_NULL, to='app.category'),
),
]
}}}

--

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

Reply all
Reply to author
Forward
0 new messages