I deleted my database. I want to start afresh with a new database. How can I do that ? I tried making a new datasource but it gives me an error while applying migrations/or migrating that it couldn't find the tables? Which is true because its an empty database.
A similar scenario would be when some one pulls a version of my code. He wouldn't have migrations or the database (untracked). How would he run the application?
Delete all the folders named 'migrations'. And go to terminal and run ./manage.py makemigrations, ./manage.py migrate --run-syncdb.
Related
I deployed my application to Heroku, but I am not able to add any data.
When I tried to open first table with data from model I received this error message:
ProgrammingError at /category/category_table
relation "tables_category" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "tables_category"
And when I tried to add first data I received this error message:
ProgrammingError at /admin/tables/category/add/
relation "tables_category" does not exist
LINE 1: INSERT INTO "tables_category" ("category_name") VALUES ('sho...
I went through similar Q/A here, but they do not solve my issue, as I have:
a) deleted all migration files from my local disk, then
b) I run: python3 manage.py makemigrations
c) I run: heroku run python3 manage.py migrate
So all should be up to date and I have this log from Heroku:
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, tables
Running migrations:
No migrations to apply.
So currently do not know what to do.
Don't delete migrations
The first step here is very dangerous:
I have... deleted all migration files from my local disk
It is very likely that the new migrations you generate won't work on your local machine. They will attempt to migrate from an empty database to whatever your current state is, but you (presumably) already have tables and maybe even data in your local database from earlier migrations.
Deleting migrations is almost never the right answer. I know a lot of people recommend it whenever you get into migration trouble, but they are wrong. Throwing away your database might feel safe during early development, but it definitely isn't desirable when you have real data later on.
I strongly suggest that you restore your old migration files, then generate new migrations on top of them.
Generating and applying migrations
When you run
python3 manage.py makemigrations
you generate new migration files on your local machine. Heroku has no idea that they exist, so running
heroku run python3 manage.py migrate
won't do anything.
After cerating migrations on your local machine, apply them locally by running python manage.py migrate to make sure they do what they're supposed to do. When you're happy with them, commit the files, redeploy to Heroku, and then run the migrations on Heroku.
I changed a field on a model class (which has no classes point to it, only one foreign key pointing out of it). Somehow, this stuffed up my migrations and it keeps saying "django.db.migrations.graph.NodeNotFoundError:" looking for migration files that do not exist.
I accidentally deleted several files in my 'migrations' folder.
My database contains a lot of data, and I do not want to break it.
Will I lose any data if I:
Remove the table that caused the problem in the first place (psql, \d, DROP TABLE tablename)
delete all my migration files
Re run the migration from the start?
./manage.py makemigrations
./manage.py migrate
Can anyone recommend another way of fixing this?
Here is the traceback:
http://dpaste.com/0Y1YDXS
Aren't you using git so that you can get your migration files back? If not, install and use it, starting now
I would suggest:
make a backup/dump of your database first, in case something goes wrong
Delete all migrations
Empty migration table in psql
call makemigrations
call migrate --fake-initial
Empty the django_migrations table:
delete from django_migrations;
Remove all the files in migrations folders in each and every app of your project.
Reset the migrations for the "built-in" apps:
python manage.py migrate --fake
Create initial migrations for each and every app:
python manage.py makemigrations your_app_name
Final step is to create fake initial migrations:
python manage.py migrate --fake-initial
See here https://micropyramid.com/blog/how-to-create-initial-django-migrations-for-existing-schema/
I'm working on an Django project, and building and testing with a database on GCP. Its full of test data and kind of a mess.
Now I want to release the app with a new and fresh another database.
How do I migrate to the new database? with all those migrations/ folder?
I don't want to delete the folder cause the development might continue.
Data do not need to be preserved. It's test data only.
Django version is 2.2;
Python 3.7
Thank you.
========= update
After changing the settings.py, python manage.py makemigrations says no changes detected.
Then I did python manage.py migrate, and now it complains about relation does not exist.
=============== update2
The problem seems to be that, I had a table name Customer, and I changed it to 'Client'. Now it's complaining about "psycopg2.errors.UndefinedTable: relation "app_customer" does not exist".
How can I fix it, maybe without deleting all files in migrations/?
================ update final
After eliminating all possibilities, I have found out that the "new" database is not new at all. I migrated on that database some months ago.
Now I created a fresh new one and migrate worked like a charm.
Again, thank you all for your suggestions.
The migration folder is your friend. No need to delete it. You can flatten migrations if you feel the migration folder is getting too large with many migration files, but you don't need to.
If you plan to use a new database in GCP, just change the settings.py file, which is typically located in the project_name/project_name/ folder. Locate the DATABASES section to reflect the new database credentials.
Once your app is pointing at the new database, run python manage.py migrate. This will build the database schema with the necessary tables to start populating the new database.
You don't need to delete any migrations/ folder, you can use the same migration files in product database, if you don't need the test data just delete the database i.e. db.sqlite3 (I guess you are using default database).
Note: Migration files don't effect your database structure(if you delete and do makemigrations or use the existing one it remains the same)
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 used the python manage.py makemigrations myApp and python manage.py migrate myApp commands to create Postgres tables in my database. Then someone manually deleted them with SQL queries.
Can I re-create these tables, without data, using terminal commands?
It depends.
Did they delete all of the tables? If so thats easy. Just rerun the
manage.py migrate.
Did they only delete new tables that you have added with migrations?
Not so bad, go into the django_migrations table, and delete the entries for the migrations that created the deleted tables from that table and rerun
manage.py migrate.
When you run makemigrations, Django creates the migration file but doesn't do anything with it in terms of touching the database.
When you run the migrate command, Django runs the alter table commands on the database and adds an entry to the django_migrations table, so it knows which migrations have been done, and which still need to be run.
If the person has deleted a random set of tables that are out of sync with your migrations its going to be a mess and its going to involve a lot of manual work to make sure your migrations and database tables are in sync.