SQL database. I dropped a table, how can I now create it? - django

To drop table I used that command:
python manage.py dbshell
.tables
DROP TABLE table_what_i_drop;
Then I tried:
python manage.py makemigrations
and:
python manage.py migrate
but the table wasn't created.
I deleted migrations folders from all apps and the dbsqlite3 file and tried again makemigrations and migrate, but databases wasn't created. Now when I tried:
python manage.py dbshell
.tables
there are no tables for any of my apps. I know that losing tables is my fault, but how I can make all databases from the beginning?

There are two steps in migrations - make migrations and migrate.
makemigrations creates the migration file based on database changes.
migrate runs the migrations and makes the database changes. It also logs what migrations have been run in the table:
django_migrations
You deleted the migrations folder,but there will still be an entry in django migrations saying that your previous migrations have run.
So to fix this, go to the django_migrations table and remove the entries related to your app (there will also be entries for Django tables, such as auth and contenttypes, so don't delete those)
(Alternately just delete the whole sqllite database and start from scratch if you don't mind loosing data in the database)

Related

why django can not create a table after deleting the table and the migration files of the app?

I am working with Django v4.* which I connected it to Postgres DB on the localhost,
I have created my model (Article) then makemigrations then migrate
then I have changed the model by adding extra field, as a result it didn't take effect so I have deleted the table and all the migrations files in articles/migrations folder apart of the __init__.py file, then I did makemigrations then migrate it create a new file 0001_initial.py but its not creating a new table into the DB, unless I drop the whole DB, which is not ideal in the production env!
I am wondering why Django is unable to create the table back again? and how I can get it created as a new table?
Roll back to your initial migration by running below command:
python manage.py migrate --fake <appname> 0001
Followed by:
python manage.py migrate <appname>
Just delete your database with
python manage.py flush
And delete migrations files
You may have mistyped the directory in the terminal

Used manage.py dbshell -> DROP TABLE to delete a database table, now I cannot recreate it

I am trying to use a custom Django management command to populate a db with scraped data. I messed this up on the first go so I deleted the table using python manage.py dbshell and then DROP TABLE player_player (Player is the name of the model). The table is now gone but when I re-run python manage.py makemigrations and python manage.py migrate it does not seem to recreate it. I tried deleting the migrations in the player application and ran python manage.py makemigrations again and it returned
Migrations for 'player':
player/migrations/0001_initial.py
- Create model Player
but when I run python manage.py migrate it says
Running migrations:
No migrations to apply
and does not recreate the Player db table.
Can anyone help with this? I was getting an error I couldn't solve so I figured I'd delete the table and try again since there was no data I cared about in there, but now think that was a poor decision.
This is because Django has a table named django_migrations that lists all the migrations that have been applied. Although you removed the table, Django still thinks it has applied the migration, because the name of the migration is listed in the table.
You can run the query:
DELETE FROM django_migrations WHERE name='0001_initial' AND app='player'
and then reapply the migration.
That being said, I would advise not to manually change the structure of the database. It might eventually result in putting the database in a state where fixing the database can be inconvenient.

Django migrate when model was previously deleted

When I do python manage.py migrate the desired model doesn't appear in my postgres database. I previously deleted all the tables and then migrated it again and it worked but I don't want that because I have loaded many data in the tables that I will have to load again. How to migrate only a certain model.
You need to delete the migration files in app/migrations first and then run makemigrations before running migrate:
python manage.py makemigration
Also make sure to delete the corresponding lines in your django_migrations table.

What is the command to force all migrations in Django?

I switched database names, and now my Django models are out of sync with my database tables. What is the command to force Django to perform all migrations to sync up the models and the tables? I don't want manage.py migrate --fake.
I fixed this by manually deleting all the migrations and running makemigrations again to get a new initial migration file. Then, I went into my database and manually dropped all the tables that Django created in the app. Finally, I deleted all of the rows in the table django.migrations that included the app name. After all that, I ran manage.py migrate and the database was in sync.
When you change something in your models you must execute:
python manage.py makemigrations
for creation new changed schema of your models. Then:
python manage.py migrate <app_name>

django migration No migrations to apply

I have screwed up my database so I tried to redo it. I did:
1) deleted all tables via sql
2) deleted the migrations folder
So when I now do manage.py makemigrations myapp it is creating the migration folder and the initial.py file, which looks fine. Also the __init__.py is there. However, if i do manage.py makemigrations myapp I always get the message "No migrations to apply."
I looked up the database in the shell and the tables are not there.
I am using Django 1.8.
Django keeps track of all the applied migrations in django_migrations table.
So just delete all the rows in the django_migrations table that are related to you app like:
DELETE FROM django_migrations WHERE app='your-app-name';
and then do:
python manage.py makemigrations
python manage.py migrate
Django keeps track of all the applied migrations in django_migrations table.
So, you can follow this method:
Delete the related rows from django_migrations.
run python manage.py migrate.
I usually ran into that issue myself. Here is the workaround I found:
Replace your database sqlite3 with this
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '<your_username>$<your_database_name>',
'USER': '<your_username>',
'PASSWORD': '<your_mysql_password>',
'HOST': '<your_mysql_hostname>',
}
}`
The fill in for can be found in the databases tab on your Pythonanywhere dashboard.
Push it to github from your Terminal and pull it down from Pythonanywhere Bash again. You may have to add this on Bash Console: pip install mysqlclient
As #Anush mentioned, Django does keep track of all of the migrations in the django_migrations table. As mentioned, you can use raw SQL to remove the migration row to reset Django's migration history for the concerned app.
DELETE FROM django_migrations WHERE app='my-app';
If you are uncomfortable deleting rows like this, then you can use Django's management command replacing my-app for the name of your app.
python manage.py migrate --fake my-app zero
That command will remove the migration history rows from the database leaving you to start your migration again from scratch. You will then need to:
Delete all of the migration files within your app's 'migrations' directory but ensure that the __init__.py file remains.
Ensure that your app's data models are set up as you need them
Make the migrations again with the command python manage.py makemigrations {your_app_name}. This will create the 0001_initial.py file within your app's migrations directory.
You will not be able to actually migrate the initial database migration because the database table already exists, so you will need to fake the migration with the command, python manage.py migrate my_app --fake-initial
Instead of run:
python manage.py migrate
just run:
python manage.py migrate --run-syncdb
If you have many problems and require a solution
Create a database backup (more than one if necessary)
My solution was to run the following query in the SQL shell
drop database database_name with (force);
This completely removes the database, be careful
It solved the error for me
Greetings
After running into this problem on Django 3.0 I spent an entire day troubleshooting this. Sharing this so that others would not need to. Most of the times running
python manage.py makemigrations
python manage.py migrate
Should solve the problem. I followed the above step after deleting the files in migrations folder (except init.py), however I was not able to solve the problem. I found that every time you run makemigrations, a python file called 000x_xxx.py is created in the migrations folder. I looked inside the file and found that all my required changes were there but it also had other create table operations that I did not need (i.e those tables had already been created). If you run migrate with --fake or --fake-initial, the whole execution will be faked (i.e executed without actually making any change in the database).
To solve the problem I opened the generated 0000x_xxx.py file and deleted the changes I wanted to make in the database and ran "python manage.py makemigrations" again. This created a new migration file with the changes I wanted. After that I ran the following command.
python manage.py migrate --fake-initial appname
This solved the problem. Not sure if this should be this complicated.