I have 4 models. I want to redo 1 one of them that I've been working on. The other 3 models have user data that I don't want to lose.
I want to entirely drop one table, and sync it up with what's in models.py.
How do I do this?
You could remove the model from models.py, and create a migration which will drop the table.
./manage.py makemigrations
Then add the model back to your models.py, and create a new migration which will recreate the model.
./manage.py makemigrations
Finally, run your migrations and you should be done.
./manage.py migrate
It's not very clear what you want to do here.
You can write new model class and delete old model class that you want to remove. After that run migrations the normal way.
It will delete the table related to deleted model class and make whatever othe changes you have defined in the models.
Related
I'm working with a few people on an app. I'm doing front end.
Recently, I've messed up migrations. After trying to fix them for a few hours, I've dropped all tables, and cloned the repo again.
Since there are no migrations files, I run manage.py makemigrations (for some reason it does not detect all apps, just one of them, and I have to call makemigrations manually for each of them).
Then, I run manage.py migrate. I get the following error:
Related model 'User.user' cannot be resolved
Since User table has OneToOneField relation to User table. Also, other tables depend on each other as well.
My take on this problem would be commening out all the fields that cause the problem, making migrations, uncommenting them, and making migrations again.
How should I fix it?
Ok, I solved this particular problem:
In User app there was another model, which referred to User. Automatically created migration file had this model before User model, so the script failed, since it could not refer to a model that is yet not created.
I solved this by editing the migration file, swapping the order of creating models - so the second model can refer to the first one.
I have two this model, which I want to move to another app.
After running this migration, I successfully could see the model under the required app.
But when I added another field, It was adding the entire model to the new migrations in new_app.
According to most tutorials, it should have added just the field.
I don't wanna fake the migrations, as it can cause issues.
Please point out my mistake.
Problem: On adding a new field in new_app model, the migrations has "CreateModel". How to avoid this?
Please help.
Migrations are persisted on your db, there's a dedicated table that keeps the track of your changes. You shouldn't modify migrations directly.
My suggest:
for development purpose you can delete your migration's files and migration's table.
Try to create your migration once you moved your model If you can't delete anything,
then you should have something like.
users/0001_mymodel.py
users/0002_mymodel_deleted.py
otherapp/0001_mymodel_added.py
otherapp/0002_adding_field.py
In the documentation it says that if I want to override the default user model in a project, I need to do so before running any migrations or running manage.py migrate for the first time. I wonder what happens if I do the opposite, which is changing the user model to a custom one AFTER having run the migrations. I only have myself registered as a user to test the functionality of my web app, if I lose it, it doesn't matter to me.
If you don't mind losing data, then there is no problem. You should drop your existing database and delete all the migrations that have been created - this is one of the few times when deleting migrations is appropriate.
Now, you can change your user class and run makemigrations again; from Django's point of view, this is now the first time.
I have a question about the order in which tables are created in a migration. As there is a ForeignKey in model B to connect to model A, I create models with order of A, B and C in models.py. Then:
python manage.py makemigrations app
There is the migration file generated to create all the models, but the order is:
- Create model B
- Create model C
- Create model A
- Add field a_name to b.
As the order in models.py really matters, but why doesn't makemigrations follow the given order?
The order in which you place your models in your models.py matters if and only if one of them references another as a ForeignKey. In such a situation the order is important and you will find the the migration does preserve the order.
What's really important is not what shows up when you do manage.py makemigrations but what happens when you do manage.py migrate there django usually figures out the correct order. If at anytime you feel that you want to control the order in which tables are created, you are free to edit the migration file (even though this is not really needed)
I had to change a field in my model from date to integer. Schemamigration could not do this so i had to delete the table (there was no data so it didn't matter). After i made the change I tried to create a new schemamigration to create the new table with the updated field. For some reason schemamigration could not find any changes. I tried to run migrate anyway and it said relation "tablename" does not exist. I tried dbsync but it skips my project. It says i have to use migration.
Any ideas of what to do / what could be causing this issue?
-Thanks
You probably still have the old migrations in the south/ folder. If you anyway start with a new database, you can as well delete the old migrations.
Create the table as before your change.
Delete the model
Run schemamigration to create the 'delete table' migration
Create correct model
Run schemamigration to create the 'create table' migration
Apply migrations