Django 1.11 Model Migration Operations doesn't apply
up vote
2
down vote
favorite
My operation inside my migration is executed, whereas nothing happened into the database concerning the payer field.
Here's my model:
class User(AbstractBaseUser, PermissionsMixin, WithCsvImportMixin):
first_name = models.CharField(_('first name'), max_length=30, blank=False)
last_name = models.CharField(_('last name'), max_length=30, blank=False)
email = models.EmailField(_('email address'),
unique=True, blank=False)
external_id = models.CharField(_('external id'), db_index=True, max_length=50, unique=True, blank=False, null=True)
is_staff = models.BooleanField(_('staff status'), default=False)
is_active = models.BooleanField(_('active'), default=True)
date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
payer = models.CharField(_('payer'), max_length=50, blank=False, default='')
The first migration concerning the payer field:
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2018-11-15 21:58
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('authenticate', '0012_auto_20181112_1631'),
]
operations = [
migrations.AddField(
model_name='user',
name='payer',
field=models.CharField(default='', max_length=50, verbose_name='payer'),
),
]
And this is the one that is executed but the result doesn't seems to be applied to the database:
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2018-11-15 22:01
from django.db import migrations, transaction
from xxx.authenticate.models import User # The xxx is here to replace the actual application name
def create_payer_names(apps, schema_editor):
with transaction.atomic():
for user in User.objects.all():
if user.external_id != "":
user.payer = user.external_id
user.save()
class Migration(migrations.Migration):
dependencies = [
('authenticate', '0013_user_payer'),
]
operations = [
migrations.RunPython(create_payer_names, migrations.RunPython.noop, atomic=True),
]
My database is already populated, and each user has an external_id
>>> from xxx.authenticate.models import User
>>> for item in User.objects.all():
... print(item.external_id)
...
043819FZAFR
762944FZAFR
285895FZAFR
671800FZAFR
924618FZAFR
068805FZAFR
232544FZAFR
130978FZAFR
412568FZAFR
600003FZAFR
254624FZAFR
417745FZAFR
068280FZAFR
031971FZAFR
141936FZAFR
>>>
And here is the empty result of previous migrations:
>>> for item in User.objects.all():
... print(item.payer)
...
>>>
Here's the output for the command : manage.py showmigrations
authenticate
[X] 0001_initial
[X] 0002_auto_20171103_1722
[X] 0003_auto_20171109_1206
[X] 0004_auto_20180329_1049
[X] 0005_auto_20180507_1123
[X] 0006_user_by_pass_generates_invoice
[X] 0007_auto_20180524_1732
[X] 0008_auto_20180530_1525
[X] 0009_auto_20180801_1644
[X] 0010_user_payment
[X] 0011_remove_user_payment
[X] 0012_auto_20181112_1631
[X] 0013_user_payer
[X] 0014_auto_20181115_2301
And when I do:
./manage.py migrate authenticate 0014
The output is the following:
Running migrations:
No migrations to apply.
Could someone explain to me what I'm doing wrong and why the migration operation isn't applied as it's supposed to be?
python django python-3.x django-migrations
add a comment |
up vote
2
down vote
favorite
My operation inside my migration is executed, whereas nothing happened into the database concerning the payer field.
Here's my model:
class User(AbstractBaseUser, PermissionsMixin, WithCsvImportMixin):
first_name = models.CharField(_('first name'), max_length=30, blank=False)
last_name = models.CharField(_('last name'), max_length=30, blank=False)
email = models.EmailField(_('email address'),
unique=True, blank=False)
external_id = models.CharField(_('external id'), db_index=True, max_length=50, unique=True, blank=False, null=True)
is_staff = models.BooleanField(_('staff status'), default=False)
is_active = models.BooleanField(_('active'), default=True)
date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
payer = models.CharField(_('payer'), max_length=50, blank=False, default='')
The first migration concerning the payer field:
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2018-11-15 21:58
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('authenticate', '0012_auto_20181112_1631'),
]
operations = [
migrations.AddField(
model_name='user',
name='payer',
field=models.CharField(default='', max_length=50, verbose_name='payer'),
),
]
And this is the one that is executed but the result doesn't seems to be applied to the database:
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2018-11-15 22:01
from django.db import migrations, transaction
from xxx.authenticate.models import User # The xxx is here to replace the actual application name
def create_payer_names(apps, schema_editor):
with transaction.atomic():
for user in User.objects.all():
if user.external_id != "":
user.payer = user.external_id
user.save()
class Migration(migrations.Migration):
dependencies = [
('authenticate', '0013_user_payer'),
]
operations = [
migrations.RunPython(create_payer_names, migrations.RunPython.noop, atomic=True),
]
My database is already populated, and each user has an external_id
>>> from xxx.authenticate.models import User
>>> for item in User.objects.all():
... print(item.external_id)
...
043819FZAFR
762944FZAFR
285895FZAFR
671800FZAFR
924618FZAFR
068805FZAFR
232544FZAFR
130978FZAFR
412568FZAFR
600003FZAFR
254624FZAFR
417745FZAFR
068280FZAFR
031971FZAFR
141936FZAFR
>>>
And here is the empty result of previous migrations:
>>> for item in User.objects.all():
... print(item.payer)
...
>>>
Here's the output for the command : manage.py showmigrations
authenticate
[X] 0001_initial
[X] 0002_auto_20171103_1722
[X] 0003_auto_20171109_1206
[X] 0004_auto_20180329_1049
[X] 0005_auto_20180507_1123
[X] 0006_user_by_pass_generates_invoice
[X] 0007_auto_20180524_1732
[X] 0008_auto_20180530_1525
[X] 0009_auto_20180801_1644
[X] 0010_user_payment
[X] 0011_remove_user_payment
[X] 0012_auto_20181112_1631
[X] 0013_user_payer
[X] 0014_auto_20181115_2301
And when I do:
./manage.py migrate authenticate 0014
The output is the following:
Running migrations:
No migrations to apply.
Could someone explain to me what I'm doing wrong and why the migration operation isn't applied as it's supposed to be?
python django python-3.x django-migrations
2
I can't see what the problem is, but you should definitely not be importing User from your actual model code; in migrations you should always get the frozen version, soUser = apps.get_model('authenticate', 'User').
– Daniel Roseman
Nov 19 at 13:48
@DanielRoseman I'm already doing it, I just changed it for this SO post in order to be more understandable!
– Exho
Nov 19 at 13:59
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
My operation inside my migration is executed, whereas nothing happened into the database concerning the payer field.
Here's my model:
class User(AbstractBaseUser, PermissionsMixin, WithCsvImportMixin):
first_name = models.CharField(_('first name'), max_length=30, blank=False)
last_name = models.CharField(_('last name'), max_length=30, blank=False)
email = models.EmailField(_('email address'),
unique=True, blank=False)
external_id = models.CharField(_('external id'), db_index=True, max_length=50, unique=True, blank=False, null=True)
is_staff = models.BooleanField(_('staff status'), default=False)
is_active = models.BooleanField(_('active'), default=True)
date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
payer = models.CharField(_('payer'), max_length=50, blank=False, default='')
The first migration concerning the payer field:
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2018-11-15 21:58
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('authenticate', '0012_auto_20181112_1631'),
]
operations = [
migrations.AddField(
model_name='user',
name='payer',
field=models.CharField(default='', max_length=50, verbose_name='payer'),
),
]
And this is the one that is executed but the result doesn't seems to be applied to the database:
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2018-11-15 22:01
from django.db import migrations, transaction
from xxx.authenticate.models import User # The xxx is here to replace the actual application name
def create_payer_names(apps, schema_editor):
with transaction.atomic():
for user in User.objects.all():
if user.external_id != "":
user.payer = user.external_id
user.save()
class Migration(migrations.Migration):
dependencies = [
('authenticate', '0013_user_payer'),
]
operations = [
migrations.RunPython(create_payer_names, migrations.RunPython.noop, atomic=True),
]
My database is already populated, and each user has an external_id
>>> from xxx.authenticate.models import User
>>> for item in User.objects.all():
... print(item.external_id)
...
043819FZAFR
762944FZAFR
285895FZAFR
671800FZAFR
924618FZAFR
068805FZAFR
232544FZAFR
130978FZAFR
412568FZAFR
600003FZAFR
254624FZAFR
417745FZAFR
068280FZAFR
031971FZAFR
141936FZAFR
>>>
And here is the empty result of previous migrations:
>>> for item in User.objects.all():
... print(item.payer)
...
>>>
Here's the output for the command : manage.py showmigrations
authenticate
[X] 0001_initial
[X] 0002_auto_20171103_1722
[X] 0003_auto_20171109_1206
[X] 0004_auto_20180329_1049
[X] 0005_auto_20180507_1123
[X] 0006_user_by_pass_generates_invoice
[X] 0007_auto_20180524_1732
[X] 0008_auto_20180530_1525
[X] 0009_auto_20180801_1644
[X] 0010_user_payment
[X] 0011_remove_user_payment
[X] 0012_auto_20181112_1631
[X] 0013_user_payer
[X] 0014_auto_20181115_2301
And when I do:
./manage.py migrate authenticate 0014
The output is the following:
Running migrations:
No migrations to apply.
Could someone explain to me what I'm doing wrong and why the migration operation isn't applied as it's supposed to be?
python django python-3.x django-migrations
My operation inside my migration is executed, whereas nothing happened into the database concerning the payer field.
Here's my model:
class User(AbstractBaseUser, PermissionsMixin, WithCsvImportMixin):
first_name = models.CharField(_('first name'), max_length=30, blank=False)
last_name = models.CharField(_('last name'), max_length=30, blank=False)
email = models.EmailField(_('email address'),
unique=True, blank=False)
external_id = models.CharField(_('external id'), db_index=True, max_length=50, unique=True, blank=False, null=True)
is_staff = models.BooleanField(_('staff status'), default=False)
is_active = models.BooleanField(_('active'), default=True)
date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
payer = models.CharField(_('payer'), max_length=50, blank=False, default='')
The first migration concerning the payer field:
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2018-11-15 21:58
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('authenticate', '0012_auto_20181112_1631'),
]
operations = [
migrations.AddField(
model_name='user',
name='payer',
field=models.CharField(default='', max_length=50, verbose_name='payer'),
),
]
And this is the one that is executed but the result doesn't seems to be applied to the database:
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2018-11-15 22:01
from django.db import migrations, transaction
from xxx.authenticate.models import User # The xxx is here to replace the actual application name
def create_payer_names(apps, schema_editor):
with transaction.atomic():
for user in User.objects.all():
if user.external_id != "":
user.payer = user.external_id
user.save()
class Migration(migrations.Migration):
dependencies = [
('authenticate', '0013_user_payer'),
]
operations = [
migrations.RunPython(create_payer_names, migrations.RunPython.noop, atomic=True),
]
My database is already populated, and each user has an external_id
>>> from xxx.authenticate.models import User
>>> for item in User.objects.all():
... print(item.external_id)
...
043819FZAFR
762944FZAFR
285895FZAFR
671800FZAFR
924618FZAFR
068805FZAFR
232544FZAFR
130978FZAFR
412568FZAFR
600003FZAFR
254624FZAFR
417745FZAFR
068280FZAFR
031971FZAFR
141936FZAFR
>>>
And here is the empty result of previous migrations:
>>> for item in User.objects.all():
... print(item.payer)
...
>>>
Here's the output for the command : manage.py showmigrations
authenticate
[X] 0001_initial
[X] 0002_auto_20171103_1722
[X] 0003_auto_20171109_1206
[X] 0004_auto_20180329_1049
[X] 0005_auto_20180507_1123
[X] 0006_user_by_pass_generates_invoice
[X] 0007_auto_20180524_1732
[X] 0008_auto_20180530_1525
[X] 0009_auto_20180801_1644
[X] 0010_user_payment
[X] 0011_remove_user_payment
[X] 0012_auto_20181112_1631
[X] 0013_user_payer
[X] 0014_auto_20181115_2301
And when I do:
./manage.py migrate authenticate 0014
The output is the following:
Running migrations:
No migrations to apply.
Could someone explain to me what I'm doing wrong and why the migration operation isn't applied as it's supposed to be?
python django python-3.x django-migrations
python django python-3.x django-migrations
edited Nov 19 at 13:53
Alasdair
177k25295304
177k25295304
asked Nov 19 at 13:43
Exho
12812
12812
2
I can't see what the problem is, but you should definitely not be importing User from your actual model code; in migrations you should always get the frozen version, soUser = apps.get_model('authenticate', 'User').
– Daniel Roseman
Nov 19 at 13:48
@DanielRoseman I'm already doing it, I just changed it for this SO post in order to be more understandable!
– Exho
Nov 19 at 13:59
add a comment |
2
I can't see what the problem is, but you should definitely not be importing User from your actual model code; in migrations you should always get the frozen version, soUser = apps.get_model('authenticate', 'User').
– Daniel Roseman
Nov 19 at 13:48
@DanielRoseman I'm already doing it, I just changed it for this SO post in order to be more understandable!
– Exho
Nov 19 at 13:59
2
2
I can't see what the problem is, but you should definitely not be importing User from your actual model code; in migrations you should always get the frozen version, so
User = apps.get_model('authenticate', 'User').– Daniel Roseman
Nov 19 at 13:48
I can't see what the problem is, but you should definitely not be importing User from your actual model code; in migrations you should always get the frozen version, so
User = apps.get_model('authenticate', 'User').– Daniel Roseman
Nov 19 at 13:48
@DanielRoseman I'm already doing it, I just changed it for this SO post in order to be more understandable!
– Exho
Nov 19 at 13:59
@DanielRoseman I'm already doing it, I just changed it for this SO post in order to be more understandable!
– Exho
Nov 19 at 13:59
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
In the migration, you should use apps.get_model to get the User model. See the docs on [data migrations] for more info.
def create_payer_names(apps, schema_editor):
User = apps.get_model('authentication', 'User')
with transaction.atomic():
...
The output of showmigrations is showing that migration 0014 has already been applied. That's why you get the No migrations to apply message.
[X] 0014_auto_20181115_2301
You could re-run the 0014 migration by faking back to the 0013 migration first.
./manage.py migrate --fake authenticate 0013
./manage.py migrate authenticate 0014
Thanks a lot, it actually works pretty well! I didn't know about--fakeIt worked on my local environment. When I'm going to push to another environment, will I need to do the same command or will it be applied automatically?
– Exho
Nov 19 at 13:57
1
I'm not sure which command you expect to be applied automatically. We used--fakebecauseshowmigrationsshowed that0014had been applied, and you wanted to undo that so you could re-run migration 0014. You should runshowmigrationsin your other environment as well, and decide whether or not you need to use--fake.
– Alasdair
Nov 19 at 14:08
Thanks for the insights !
– Exho
Nov 19 at 14:17
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
In the migration, you should use apps.get_model to get the User model. See the docs on [data migrations] for more info.
def create_payer_names(apps, schema_editor):
User = apps.get_model('authentication', 'User')
with transaction.atomic():
...
The output of showmigrations is showing that migration 0014 has already been applied. That's why you get the No migrations to apply message.
[X] 0014_auto_20181115_2301
You could re-run the 0014 migration by faking back to the 0013 migration first.
./manage.py migrate --fake authenticate 0013
./manage.py migrate authenticate 0014
Thanks a lot, it actually works pretty well! I didn't know about--fakeIt worked on my local environment. When I'm going to push to another environment, will I need to do the same command or will it be applied automatically?
– Exho
Nov 19 at 13:57
1
I'm not sure which command you expect to be applied automatically. We used--fakebecauseshowmigrationsshowed that0014had been applied, and you wanted to undo that so you could re-run migration 0014. You should runshowmigrationsin your other environment as well, and decide whether or not you need to use--fake.
– Alasdair
Nov 19 at 14:08
Thanks for the insights !
– Exho
Nov 19 at 14:17
add a comment |
up vote
2
down vote
accepted
In the migration, you should use apps.get_model to get the User model. See the docs on [data migrations] for more info.
def create_payer_names(apps, schema_editor):
User = apps.get_model('authentication', 'User')
with transaction.atomic():
...
The output of showmigrations is showing that migration 0014 has already been applied. That's why you get the No migrations to apply message.
[X] 0014_auto_20181115_2301
You could re-run the 0014 migration by faking back to the 0013 migration first.
./manage.py migrate --fake authenticate 0013
./manage.py migrate authenticate 0014
Thanks a lot, it actually works pretty well! I didn't know about--fakeIt worked on my local environment. When I'm going to push to another environment, will I need to do the same command or will it be applied automatically?
– Exho
Nov 19 at 13:57
1
I'm not sure which command you expect to be applied automatically. We used--fakebecauseshowmigrationsshowed that0014had been applied, and you wanted to undo that so you could re-run migration 0014. You should runshowmigrationsin your other environment as well, and decide whether or not you need to use--fake.
– Alasdair
Nov 19 at 14:08
Thanks for the insights !
– Exho
Nov 19 at 14:17
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
In the migration, you should use apps.get_model to get the User model. See the docs on [data migrations] for more info.
def create_payer_names(apps, schema_editor):
User = apps.get_model('authentication', 'User')
with transaction.atomic():
...
The output of showmigrations is showing that migration 0014 has already been applied. That's why you get the No migrations to apply message.
[X] 0014_auto_20181115_2301
You could re-run the 0014 migration by faking back to the 0013 migration first.
./manage.py migrate --fake authenticate 0013
./manage.py migrate authenticate 0014
In the migration, you should use apps.get_model to get the User model. See the docs on [data migrations] for more info.
def create_payer_names(apps, schema_editor):
User = apps.get_model('authentication', 'User')
with transaction.atomic():
...
The output of showmigrations is showing that migration 0014 has already been applied. That's why you get the No migrations to apply message.
[X] 0014_auto_20181115_2301
You could re-run the 0014 migration by faking back to the 0013 migration first.
./manage.py migrate --fake authenticate 0013
./manage.py migrate authenticate 0014
answered Nov 19 at 13:47
Alasdair
177k25295304
177k25295304
Thanks a lot, it actually works pretty well! I didn't know about--fakeIt worked on my local environment. When I'm going to push to another environment, will I need to do the same command or will it be applied automatically?
– Exho
Nov 19 at 13:57
1
I'm not sure which command you expect to be applied automatically. We used--fakebecauseshowmigrationsshowed that0014had been applied, and you wanted to undo that so you could re-run migration 0014. You should runshowmigrationsin your other environment as well, and decide whether or not you need to use--fake.
– Alasdair
Nov 19 at 14:08
Thanks for the insights !
– Exho
Nov 19 at 14:17
add a comment |
Thanks a lot, it actually works pretty well! I didn't know about--fakeIt worked on my local environment. When I'm going to push to another environment, will I need to do the same command or will it be applied automatically?
– Exho
Nov 19 at 13:57
1
I'm not sure which command you expect to be applied automatically. We used--fakebecauseshowmigrationsshowed that0014had been applied, and you wanted to undo that so you could re-run migration 0014. You should runshowmigrationsin your other environment as well, and decide whether or not you need to use--fake.
– Alasdair
Nov 19 at 14:08
Thanks for the insights !
– Exho
Nov 19 at 14:17
Thanks a lot, it actually works pretty well! I didn't know about
--fake It worked on my local environment. When I'm going to push to another environment, will I need to do the same command or will it be applied automatically?– Exho
Nov 19 at 13:57
Thanks a lot, it actually works pretty well! I didn't know about
--fake It worked on my local environment. When I'm going to push to another environment, will I need to do the same command or will it be applied automatically?– Exho
Nov 19 at 13:57
1
1
I'm not sure which command you expect to be applied automatically. We used
--fake because showmigrations showed that 0014 had been applied, and you wanted to undo that so you could re-run migration 0014. You should run showmigrations in your other environment as well, and decide whether or not you need to use --fake.– Alasdair
Nov 19 at 14:08
I'm not sure which command you expect to be applied automatically. We used
--fake because showmigrations showed that 0014 had been applied, and you wanted to undo that so you could re-run migration 0014. You should run showmigrations in your other environment as well, and decide whether or not you need to use --fake.– Alasdair
Nov 19 at 14:08
Thanks for the insights !
– Exho
Nov 19 at 14:17
Thanks for the insights !
– Exho
Nov 19 at 14:17
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375942%2fdjango-1-11-model-migration-operations-doesnt-apply%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
I can't see what the problem is, but you should definitely not be importing User from your actual model code; in migrations you should always get the frozen version, so
User = apps.get_model('authenticate', 'User').– Daniel Roseman
Nov 19 at 13:48
@DanielRoseman I'm already doing it, I just changed it for this SO post in order to be more understandable!
– Exho
Nov 19 at 13:59