I have an existing django project and need to create an instance of it in a new environment with new database. I have the database connection configured in the settings file. The schema does not exist. If I run the manage.py migrate command, does it also create the schema? It looks like it assumes the schema already exists because I am getting an error django.db.utils.OperationalError: (1049, "Unknown database 'my_db'"). Just wondering if I have to create the database first or if some django command is available to create it if it does not exists.
I can create the schema manually via sql script if it's not doable via python django command.
As already pointed out in the comments to your question, the missing database is the problem, not the schema. You have to create the database first, which might involve setting the user permissions for the new database. After that, the manage.py migrate command will work just fine and create the schema for you.
Related
Setting up a mysql database from an existing dump is very easy
# open prompt cd into the directory where the dump is located
# login to mysql
create database mydb
use mydb
source mydb.sql
And all is done!
But the problem comes with Django. Django has troubles with existing schemes and data that have not been created from models and migration system. And in Django, everything must come from models and migrations.
I perfectly know that I have this option
python manage.py inspectdb > models.py
But this is only suitable when creating a blank new Django project using existing database.
What if I want to run an existing Django project to another machine and the Django project uses a database? The project already has existing models with some manage=false set, I cannot simply inspectdb it. And if I restore the database outside Django, the Django ORM will no longer recognize it.
Then what is the correct way to restore a database from a database backup for a project with existing models and migrations?
If the database that you are restoring was previously managed by django, you should encounter no problems simply restoring the database with your three-step process. Django's migration history is stored as a table in any database that it manages. As long as that table makes it over, your new migrations going forward should work fine.
I have an database dump file of my PostgreSQL database that was created by pg_dump.
I transfer this file to a new server. I also transferred all the relevant models.py files to this never server. I used the following command to load in all the data on the new server.
gunzip -c dump_file | psql -p port db_name db_user
I configured Django to access this database and I can query the data using manage.py shell_plus. For eaxmple, I can run Images.objects.all().count()which returns the correct number, i.e. the same number of image objects as were present on the old server.
Considering this is a different server, it does not contain any of the original migration files.
I used ./manage.py makemigrations to create the migrations files. This appears to work properly and migration files are created for each of my installed_apps.
Next, I run ./manage.py migrate. This leads to the following error:
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration shapes.0001_initial is applied before its dependency bsdfs.0002_auto_20200527_1647 on database 'default'.
All the answer on SO say to simply uncomment shapes from the installed_apps list, but this does not work because this leads to the following error when I'm importing it in the models.py. The models are extrmely big, and uncommenting each instance where shapes is being used it not possible.
RuntimeError: Model class shapes.models.Material doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS
What can I do to migrate the database properly?
I suppose you didn't have any migrations for the old db.
I assume pg_dump contained all the table-linkage information.
In this case, after running makemigrations you could try and create a table called django_migrations and manually insert a row that tells django that the 'initial migration' already ran (since your db is in sync with your code).
And when you run migrate, it should say, there's nothing to migrate.
And if later you change your model, the new migrations should be compatible, because your db is in sync now with your code.
If your app is 'contenttypes' and the filename of the migration is '0001_initial.py', you just set some past date for the applied column.
I changed the database from SQLite3 to PostgreSQL in my Django project. Is it possible to store my new database in the GitHub repository so that after cloning and running by the command
python manage.py runserver
the project has started with the whole database?
You cannot save the database as such, you can instead create fixtures to be run. Whenever someone will clone the project, he/she can simple run those fixtures to populate the database.
I am working on a tutorial on Django
When I invoke the interactive shell to access the database API using the command python manage.py shell, I receive the following prompt
In [1]:from music.models import Album, Song
In [2]: Album.objects.all()
Then the next line gives me this
OperationalError: no such table: music_album
Like the error says, it means that your database has no tables that correspond to these models.
Typically that means that you forgot to migrate your database. By running:
python manage.py makemigrations
Django will construct files (in someapp/migrations/) these files describe the steps that have to be carried out such that tables are constructed with the correct columns, or later in the progress columns can be renamed, tables removed, new tables constructed. If you run the command, you will see new files popping up that describe these migrations.
But now you still did not migrate the database itself, you only constructed the files. It is for example possible that you want to add custom migrations yourself. In case the migration files are correct, you can run
python manage.py migrate
to change the database accordingly.
In order to run the migrations, the database has to be specified correctly in the settings.py file. But since you obtain an OperationalError that complains about the table not being found, I think this has already been handled (correctly).
See the Django topic on migrations [Django-doc] for more information.
Configure your database connection in your settings.py and, after creating Django models, you have to execute two commands:
python manage.py makemigrations
python manage.py migrate
Then tables will be created in the database and you will be able to use then in the shell
I was able to set up a Custom User using AbstractUser.
I'm trying to find a way to use Django's Password Reset Class to reset my users. So I implemented it the way it is normally used but I get the following error:
ProgrammingError: column app_user.date_joined does not exist
or
app_user.is_active does not exist
I shouldn't have to add any of these fields to my AbstractUser and I'm sure there is a way to override the Django auth. Does anyone know how?
EDIT: I've already migrated all my databases.
Your database is not migrated.
You have to run python manage.py migrate to migrate the DB.
If that was never run, than you have to make the migrations before applying them.
python manage.py makemigrations
python manage.py migrate