How to create a new table using model
Solution 1
First, create a backup of your database. Copy it to your development machine. Try things out on that. That way it doesn't matter if it does go "boom" for some reason.
The first thing to do is
python manage.py showmigrations
This shows all the existing migrations, and it should show that they have been applied with an [X]
.
Then,
python manage.py makemigrations
Makes a new migration file for your new model (name 00004_...).
Then do
python manage.py migrate
to apply it. To undo it, go back to the state of migrations 00003, with
python manage.py migrate <yourappname> 00003
Solution 2
There are two steps to migrations in Django.
./manage.py makemigrations
will create the migration files that you see - these describe the changes that should be made to the database.
You also need to run
./manage.py migrate
this will apply the migrations and actually run the alter table commands in SQL to change the actual database structure.
Generally adding fields or tables won't affect anything else in the database. Be more careful when altering or deleting existing fields as that can affect your data.
The reason for two steps is so that you can make changes on a dev machine and once happy commit the migration files and release to your production environment. Then you run the migrate command on your production machine to bring the production database to the same state as your dev machine (no need for makemigrations on production assuming that your databases started the same).
Solution 3
My question is now how do I "create" this boom table using the migration script and the boom model?
./manage.py makemigrations
I am worried that I might accidentally disrupt anything that is already in DB.
The whole point of migrations, is that it doesn't
I know that it has something to do with manage.py and running migrate or runmigration
For more information please refer to : https://docs.djangoproject.com/en/1.10/topics/migrations/
And rest assured that your database will not go boom! :-)
Solution 4
I solved it simply, changing the name of the new model to the original name, and then I checked if there is the table in the database, if not, I just create a new table with the old name with just a field like id. And then clear migrations and create new migrations, migrate and verify table was fixed in DB and has all missing fields. If it still doesn't work, then change the model name back to a new one. but when django asks you if you are renaming the model you should say NO to get the old one removed properly and create a new one.
This type of error usually occurs when you delete some table in dB manually, and then the migration history changes in the tables are lost. But it is not necessary to erase the entire database and start from scratch.
Undefined Variable
Updated on June 14, 2022Comments
-
Undefined Variable almost 2 years
So I have this django installation in which there are a bunch of migration scripts. They look like so:
00001_initial.py 00002_blah_blah.py 00003_bleh_bleh.py
Now I know these are "database building" scripts which will take stuff defined in
models.py
and run them against the db to "create" tables and stuff.I want to create a new table(so I created its definition in models.py). For this, I have copied another model class and edited its name and fields and it is all fine. Lets call this new model class 'boom'.
My question is now how do I "create" this boom table using the migration script and the boom model?
I am worried that I might accidentally disrupt anything that is already in DB. How do I run the migration to create only boom table? How do I create a migration script specifically for it?
I know that it has something to do with
manage.py
and runningmigrate
orrunmigration
(or is itsqlmigrate
?...im confused). While creating the boom table, I dont want the database to go boom if you know what I mean :) -
Undefined Variable over 7 yearsThank for for the answer. I did RTFM but sadly it is not a silver bullet :) Some of us do not get it as well as others do, you know? else my granny would be a coder reading them...
-
e4c5 over 7 yearsSorry, changed the last part of my answer. Hopefully we can still make a good coder out of your grandma :-)
-
Undefined Variable over 7 yearsThank you sir. You are wonderful. This is exactly what I wanted - precise and well explained. I wish I could +1 you more than once!
-
Undefined Variable over 7 yearsThe --list option is a such a handy tool! Thank you for your patience in explain it well.
-
Undefined Variable over 7 yearsI have to wait, so says SO :)
-
Undefined Variable over 7 yearsThank you for your reply and the links...I will read them once more. This time I will understand better based on the answers you guys have given me here!
-
Undefined Variable over 7 yearsone rather silly question if I may....how do I find <yourappname> ? is it in some setting/config file ?
-
RemcoGerlich over 7 yearsUsually it's the name of the directory that the models.py is in. It should be listed in the INSTALLED_APPS setting in settings.py. Migrate --list also groups the migrations by app name.
-
Undefined Variable over 7 yearsI am not sure if I am doing something wrong...I got following error when running the migrate --list: "manage.py migrate: error: unrecognized arguments: --list"
-
e4c5 over 7 yearsall the best with your project.
-
Undefined Variable over 7 yearsyes --help shows a long help manual. Django version is 1.10.4..im reading the help manual now..
-
RemcoGerlich over 7 yearsOh crap, I see that --list was deprecated and is removed in 1.10. Serves me right for only using LTS versions... you need showmigrations now.