45👍
You have to clear admin, auth, contenttypes, and sessions from the migration history and also drop the tables. First, remove the migration folders of your apps and then type the following:
python manage.py migrate admin zero
python manage.py migrate auth zero
python manage.py migrate contenttypes zero
python manage.py migrate sessions zero
Afterwards, you can run makemigrations accounts
and migrate accounts
.
43👍
The solution is to undo your existing migrations that depend on AUTH_USER_MODEL as mentioned in this answer. In case you are trying to undo migrations for admin, auth, contenttypes and sessions and you get an error like:
ERRORS:
auth.User.groups: (fields.E304) Reverse accessor for ‘User.groups’ clashes with reverse accessor for ‘Profile.groups’.
….
- First of all comment out/undo
AUTH_USER_MODEL
in settings.py if you had changed that. - Secondly comment out/undo your django app that contains new AUTH_MODEL from
INSTALLED_APPS
in settings.py. - Now you should be able to undo migrations for auth, admin, contenttypes and sessions as:
python manage.py migrate admin zero
python manage.py migrate auth zero
python manage.py migrate contenttypes zero
python manage.py migrate sessions zero
- Now add your auth model app to
INSTALLED_APPS
and setAUTH_USER_MODEL
in your settings.py again. -
Run:
python manage.py migrate AUTH_APP
, you may need to make migrations for your auth model app as well:python manage.py makemigrations AUTH_APP
-
Apply all migrations that you undo by:
python manage.py migrate
.
You are all done.
Note: You will lose all existing users present in database.
- [Django]-Why do we need to use rabbitmq
- [Django]-Can't compare naive and aware datetime.now() <= challenge.datetime_end
- [Django]-How to reverse the URL of a ViewSet's custom action in django restframework
9👍
As in my particular case, the other answers did not help (the error still occured even after I tried to drop the tables with migrate ... zero
and even after I deleted the migrations folder), the following helped, but I was at the very beginning and therefore it was no problem to just delete the db.sqlite3
file which is created whenever you migrate the first time. (Depending on your settings.py you might have a different database-file).
You really can only do this if you are sure that you don’t lose important data from your database file (e.g. you do not yet have much information stored in the database and it is not difficult to start over again), and you will need to migrate everything again.
- [Django]-Why does Django's render() function need the "request" argument?
- [Django]-Redirect to Next after login in Django
- [Django]-Setting Django up to use MySQL
5👍
Delete the existing all the tables from data base.[Note : data will be lost]
Delete pycache and migrations from all the apps.
Run migrations for your relative app:
python manage.py makemigrations users
Migrate the tables to database:
python manage.py migrate
- [Django]-List_display – boolean icons for methods
- [Django]-Using JSON in django template
- [Django]-Django values_list vs values
4👍
You need to run:
python manage.py makemigrations accounts
Before executing the initial manage.py migrate
(by initial I mean at the very first time you run migrate
on your project)
It is recommended to set up your custom User model at the start of your project so you’ll have the "accounts" app migrated at the same time as the admin,auth,contenttypes,sessions tables are created.
But if you have created your tables already, then you should follow the instructions as @krishna-chandak described: https://stackoverflow.com/a/53599345/5950111
You can read the docs : https://docs.djangoproject.com/en/2.0/topics/auth/customizing/#using-a-custom-user-model-when-starting-a-project
- [Django]-Why does django run everything twice?
- [Django]-Get distinct values of Queryset by field
- [Django]-Aggregate (and other annotated) fields in Django Rest Framework serializers
4👍
There’s a django_migrations table in your database after your previous migration which is the cause of this inconsistency.
Solution: Deleting the django_migrations table from your database.
delete the migration folder from your apps
and then perform
python3 manage.py makemigrations
python3 manage.py migrate
- [Django]-In Django models.py, what's the difference between default, null, and blank?
- [Django]-In Django, how do I check if a user is in a certain group?
- [Django]-How to override css in Django Admin?
3👍
I know it’s rather an old question, but for people googling this topic like me today, here is a solution without deleting migrations, dropping the tables, and other nasty stuff)
https://www.caktusgroup.com/blog/2019/04/26/how-switch-custom-django-user-model-mid-project/
- [Django]-How to show a PDF file in a Django view?
- [Django]-Django-nonrel + Django-registration problem: unexpected keyword argument 'uidb36' when resetting password
- [Django]-Altering one query parameter in a url (Django)
1👍
I also had the same problem. I followed the steps:
- In
models.py
, i setup basicUser
model
# accounts/models.py
class User(AbstractBaseUser):
class Meta:
db_table = 'auth_user'
- Then, i ran
makemigrations
command to generate migration file
$ python manage.py makemigrations accounts
Migrations for 'accounts':
accounts/migrations/0001_initial.py:
- Create model User
- Next step, i inserted record has 0001_initial to
django_migrations
table
$ echo "INSERT INTO django_migrations (app, name, applied) VALUES ('accounts', '0001_initial', CURRENT_TIMESTAMP);" | python manage.py dbshell
- Update lastest in model
# accounts/models.py
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(
unique=True,
max_length=254,
)
mobile_number = models.IntegerField(unique=True)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = UserManager()
...
...
class Meta:
db_table = 'auth_user'
...
...
- I need makemigrations again
After run makemigrations, i had the next migration file.
0002_....py
- Migrate agains
python manage.py migrate.
- [Django]-Using django-admin on windows powershell
- [Django]-Django query filter with variable column
- [Django]-Error: [ngModel:datefmt] Expected `2015-05-29T19:06:16.693209Z` to be a date – Angular
1👍
What worked for me was a solution that I pieced togeather from all the diffretent solutions given here.
I check if the database exists since I don’t have an issue with an existing database, only when the database is empty.
# check if the database exists
db_ok=false
if python ./manage.py check; then
db_ok=true
fi
if [ $db_ok = true ]; then
# database exists: do a normal migrate
python ./manage.py migrate
else
# database does not exists, make and migrate users then a migrate and cleanup of the users migraton
python ./manage.py makemigrations users
python ./manage.py migrate users
python ./manage.py migrate
rm -r users/migrations/
fi
- [Django]-Http POST drops port in URL
- [Django]-Converting timezone-aware datetime to local time in Python
- [Django]-How do I use an UpdateView to update a Django Model?
0👍
I had similar problem, where I have to introduce the custom user model in the middle of the project. So following steps helped me to solve the issue without table drop or data loss.
(1) Create an initial empty migration for the app (‘accounts’)
python manage.py makemigrations accounts --empty
(2) Run migrate
python manage.py migrate
(3) Update the timestamp in the django_migrations table in the database , for ‘accounts’ initial migration to the ‘admin’ initial timestamp.
UPDATE django_migrations SET applied=<<admin 0001_initial date>> WHERE app='accounts' and name='0001_initial';
(4) Now create your Custom User model (derived from AbstractUser) with out any fields, and set the table name to auth_user. You are basically re-using the existing auth user table in database.
class User(AbstractUser):
class Meta:
db_table = 'auth_user'
(4) Now run migration, and copy the migration to 0001_initial and remove ‘0001_initial’ from the dependency array. Also remove the newly created migration file.
python manage.py makemigrations accounts
cp accounts/migrations/0002_user.py accounts/migrations/0001_initial.py
edit the file 0001_initial.py
rm accounts/migrations/0002_user.py (remove migration file)
(5) Now add your custom fields, run makemigrations and migrate as usual.
- [Django]-Where to put Django startup code?
- [Django]-Get object by field other than primary key
- [Django]-How can I change a Django form field value before saving?
0👍
First, dump your data to a file data.json
using this command :
python manage.py dumpdata app1.model1 app2.model2 ......
After you run this command:
python manage.py makemigrations
Just remove the db.sqlite3
file using the command
rm db.sqlite3
then run the command:
python manage.py migrate
then run the command:
python manage.py loaddata data.json
It worked for me.
- [Django]-"Too many SQL variables" error in django with sqlite3
- [Django]-Django – Render the <label> of a single form field
- [Django]-Cancel an already executing task with Celery?
-1👍
Migrate zeroing didn’t help me. I had to drop the whole database:
sudo -u postgres psql
drop database YOURDATABASENAME;
create database YOURDATABASENAME;
Then:
python ./manage.py makemigrations MYAPPNAME
python ./manage.py migrate MYAPPNAME
python ./manage.py migrate
And after these I got forward..
- [Django]-Celery : Execute task after a specific time gap
- [Django]-Django multiple and dynamic databases
- [Django]-How do I raise a Response Forbidden in django