I've just started working with Django, and when setting up my models and db I picked a name which I would like to change now. Is it OK to just edit models.py (rename the class), then run makemigrations and migrate on it? I have a table set up in the db (SQLite), but no entries yet.
I'm new to database migration too. Does this cover what I want to do?
Thanks.
After renaming of model in the models.py, run makemigrations. The question should appear "Did you rename the xxx model to yyy? [y/N]" Press y and it will be enough in your case.
UPD:
In general, you should run makemigrations and migrate every time when you change code in models.py. You should manually edit migrations files (created by makemigrations) only in few cases, when the Django isn't smart enough to understand what you want to do.
Related
I've deleted both table and 0001_initial.py migration file from a model. I didn`t think I will need this model anymore, but now I do need it.
The problem is that python manage.py makemigrations doesn't create a table with the name of deleted table and therefore my migrations are only displayed in migrations files. But they don`t affect database.
How can I create or restore that table again or should I delete the whole database and restore everything afterwards?
Thanks in advance!
It can sound stupid but did you check bin in your machine?
When using the Django Admin panel, I am able to add new instances to Users and Groups. The same thing goes for all my apps, except for one. This one app, resultregistration, always gives me a ProgrammingError whenever I try to add a new model. For example, this app has a Competition-model where data about a sport competition should be stored.
When I click "+ Add" I am taken to the correct site where I can add a new competition. However, when I press save I get:
ProgrammingError at /admin/resultregistration/competition/add/
column "competition_category" of relation "resultregistration_competition" does not exist
LINE 1: INSERT INTO "resultregistration_competition" ("competition_c...
Of course, I assume something is wrong with the migrations. However, I have run python manage.py makemigrations appname, and python manage.py migrate appname, and that works fine. I get a message that there are "No changes detected", and "No migrations to apply".
I have tried solutions posted on SO, but none of them worked.
What is the general reason for this error? And does anyone know what could be wrong in this specific case? Could one get this error if something is wrongly defined in the model? Or does it have to be a migration problem?
Thank you so much! Any help would be truly appreciated. Also, I am using PostgreSQL, if that helps.
Make sure you have _ init _.py file under the migrations folder for that paticular app.
Running manage.py makemigrations always makes a migration file if there are any changes in your models.py
If nothing works and there isnt much data present in your database, end resolution is to delete all migrations files (if any) for that app or for the project, in terminal type "sudo -su postgres" then type "psql".
Drop your database and create a new one. Run manage.py makemigrations to check if migration file has been created or not. Then migrate with manage.py migrate
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 running migrate or runmigration (or is it sqlmigrate?...im confused). While creating the boom table, I dont want the database to go boom if you know what I mean :)
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
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).
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! :-)
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.
I've wasted all my day trying to make this work, but I couldn't.
Here's what I've done so far:
Created an app and its models (no south at this point)
Installed South on my system
Added 'south' to my project settings
Ran manage.py syncdb to create south migration tables
Converted my app to south using: manage.py convert_to_south myapp. This created a migrations folder and an initial migration file.
Changed my model (added a new field)
Ran manage.py schemamigration myapp --auto
It says NOTHING SEEMS TO HAVE CHANGED.
Here's my original model:
https://gist.github.com/kustomrtr/8f4f4262634904b53d5f
Here's my MODIFIED model (added line 41):
https://gist.github.com/kustomrtr/edab10975803c74b869a
Here's my initial migration file (created when I converted my app to south):
https://gist.github.com/kustomrtr/2a4884be3177225a45e7
Can you help me please!
Thanks in advance.
Someone pointed that the Manage = False in my models could cause the trouble. I tried commenting those lines and guess what:
C:\Users\Kevin\Desktop\difundelo>python manage.py schemamigration registros --auto
? The field 'Post.image' does not have a default specified, yet is NOT NULL.
? Since you are adding this field, you MUST specify a default
? value to use for existing rows. Would you like to:
? 1. Quit now, and add a default to the field in models.py
? 2. Specify a one-off value to use for existing columns now
? Please select a choice:
Voilá! It worked! I can finally keep working on my project!
PS: I don't know why the guy who commented the solution, deleted his comment. Thank you anonymous!!!
The problem is pretty self-explanatory in the title. Do I need to do that or I just need to edit the existing migration file?
Yes, Django won't recognize the field if you change the name. I will say that the "field does not exist", so YES, you have to run Django's South migrate / schemamigration as you asked.
Datatype YES as well. Django may be okay at first if you only change the field type depending, but may run into problems later depending on what you have in that field.
You need to do a schemamigration every time you change your models.
On every call of python manage.py migrate command south record number of the latest migration applied into database migrationhistory table. So if you just change existing migration it won't be applied because south would think it's already applied.
You can make a backward migration, fix next migration, even delete it and make a new one and only then migrate forward.