- I’m using Django 4.2 with PostgreSQL
- The last Django migration of the
django.contrib.auth.models.User
app is 0012_alter_user_first_name_max_length.py
- The only change to the maxlength of the “username” property since the initial migration 0001_initial.py was from 30 to 150
- Looking at the
User
model, the username
property does have maxlength=150
- However, when running the command
web/manage.py makemigrations auth
, another migration 0013_alter_user_username.py (code below) is created in an attempt to change the maxlength from 150 to 255, but maxlength=255
is nowhere to be found in the code
- And even though I apply this additional migration (
web/manage.py migrate auth
) countless times, no change occurs in the auth_user
table.
What could this be? I would appreciate it if someone could guide me.
0013_alter_user_username.py
# Generated by Django 4.2.13 on 2024-11-04 18:44
import django.contrib.auth.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('auth', '0012_alter_user_first_name_max_length'),
]
operations = [
migrations.AlterField(
model_name='user',
name='username',
field=models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=255, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username'),
),
]
Welcome @leonardotrp !
Have you verified, in the database, what the length of that field currently is?
Does the output of a showmigrations
show this migration as having been applied?
Have you made any model changes in your project potentially affecting this? (What other models were created since your initial build?)
What problem or situation is this issue causing? (Or is this just an issue of curiosity?)
Hello @KenWhitesell. Thank you for your reply.
In the database, the size is 150 and is consistent with the Django 4.2 code (django.contrib.auth.models.AbstractUser.username
).
If the “username” property has maxlength=150
, both in the code and in the database, I understand that Django should not create any new migrations. Does this make sense to you too? Remember that the last migration that came with Django 4.2 starts with 0012 (django/contrib/auth/migrations/
).
As for other models in the application, the User model is only referenced as models.ForeignKey
or models.OneToOneField
In the general or common case, yes. But I can envision situations where third-party packages could reconfigure the User object.
what is your AUTH_USER_MODEL?
Also, check that you are not using the default user model.
Hi @white-seolpyo
I didn’t find any override for this settings.AUTH_USER_MODEL
property. And in the debug below, the default value auth.User
is appearing:
Hello @KenWhitesell Thank you again for your willingness to help me.
Could you show me some possible situations that could reconfigure the django.contrib.auth.User
model?
I don’t have anything handy, no.
But to repeat my earlier questions:
If the migration has been applied, what happens if you unapply the migration? Does anything happen?
If after unapplying the migration, if you then delete the migration file and run makemigrations
, does it recreate this migration?
And, what third-party packages do you have in your INSTALLED_APPS setting?
Answering your questions:
-
Does the output of a showmigrations
show this migration as having been applied?
Answer: Yes. After Django creates this new migration (with maxlength=255
) I apply it and the showmigrations
command signals that it has been applied. However, curiously, the column’s maxlength does not change from 150 to 255.
-
What problem or situation is this issue causing? (Or is this just an issue of curiosity?)
Answer: Just curious. Firstly, because Django is creating this additional migration. And secondly, because its application is not reflected in the database. If I don’t run makemigrations
from the auth
app, the application works without any problems.
-
If the migration has been applied, what happens if you unapply the migration? Does anything happen?
Answer: Nothing happens, whether I apply the migration or go back to the previous migration (unapply). For this reason, if I insist, Django ends up creating several identical migrations later, which have no effect.
-
And, what third-party packages do you have in your INSTALLED_APPS setting?
Answer: You helped me find the cause of the problem. There is indeed a third-party library that is overriding Django’s User model. Check this out:
from django.contrib.auth.models import User
for field in ('username', 'first_name', 'last_name', 'email'):
f = User._meta.get_field(field)
f.max_length = 255
Problem discovered. The cause of the problem was quite hidden. Django didn’t like this change made by the third-party library to the User model.
I apologize for the confusion. If you think it’s appropriate, you can remove this topic. I really appreciate your help.
I’m very glad you found the answer.
Actually, I think this is exactly the type of issue that belongs here.
Having a third-party app alter a model is not something most people think about as being the cause of an issue.
1 Like
i mean that what is your AUTH_USER_MODEL in settings.py, not migration.
you seems like want to use custom user model, but django provide default user model.