Writing migrations to wrong database entirely

57 views
Skip to first unread message

David Crandell

unread,
Nov 14, 2023, 9:14:22 AM11/14/23
to Django users

Howdy, I have been working with Django for about three years now and have developed a couple business grade apps which are deployed and doing great.

The other day, I created a new project with a new database, etc.

I built out the initial models and migrated them to the proper database. However, subsequent migrations are going to a database I built for another app entirely. The two databases aren't on the same host, don't have similar names - they have nothing in common.

How is this possible? Is there some connection cache I need to clear? I haven't even had any of the other projects open or running locally- nothing.

I'm baffled.

Lutaaya Jamil

unread,
Nov 14, 2023, 10:08:50 AM11/14/23
to Django users
Are you using the database_url your environment variables? Maybe the file is same

Ahmed Iftikhar

unread,
Nov 14, 2023, 10:20:32 AM11/14/23
to Django users
It's possible that there is a migration state issue. Try running the following commands:

python manage.py migrate <app_name> zero
python manage.py migrate  

This will revert all migrations for the specified app and then migrate all apps to the initial state. Be cautious, as this will reset your database.
If you are using a database router to manage multiple databases, ensure that it is correctly configured. The DATABASE_ROUTERS setting in settings.py should point to a valid router class.
Django maintains a migration cache. Try clearing it by running:

python manage.py migrate --clear

If you are using a connection pool, it might be holding on to a connection from a previous migration. Restarting your development server or the Django shell might help in such cases.
Ensure that you are using a compatible version of Django. Sometimes issues can arise due to compatibility problems between Django versions and database backends.
Verify that environment variables related to your database settings are not set unexpectedly. 

David Crandell

unread,
Nov 14, 2023, 10:56:02 AM11/14/23
to Django users

Found the issue. I had a DATABASE_URL defined in my environment variables for one of my projects and my other project was somehow using this even though settings.py had its own definition under DATABASES.

I temporarily changed the DATABASE_URL var to a different name to eliminate it from the equation and then to be sure, I ran python manage.py migrate <app_name> zero on each app, then ran initial migrations for the auth and django tables and then makemigrations on my own models. It cleared everything up and migrated to the proper DB.

Thank y'all for your help.


T

Lutaaya Jamil

unread,
Nov 14, 2023, 11:23:42 AM11/14/23
to Django users
I guessed that would be the issue as I normally run into it at times

David Crandell

unread,
Nov 14, 2023, 12:46:08 PM11/14/23
to django...@googlegroups.com
Again, thank you so much.

You are awesome.


--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/8qOfTvyVAVE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9b1c8381-063b-4971-9439-74b0dc0bde13n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages