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?
Related
I kind of know why when I do migrate it gives me the message of no migrations to apply but I just don't know how to fix it
This is what happens.
I added a new field named update into my model fields.
I did a migration which created a file called 0003_xxxxx.py then I did a migrate now this worked fine.
But then because of some reason, I have to remove the update field from the same model.
So I removed it (more likey commented instead of really delete the code) then I did migration and migrate which removed the field in db. (created 0004_xxxxx.py)
But sigh....some reason I have to add the field back again (this is why I only commented out) but then before I do the migration
I removed the 0003_xxxx.py and 0004_xxxx.py files I wanted to remove those two files because this is actually the same or almost the same config as 0003_xxxx.py so I feel it's pointless having the 0003_xxxx.py and 0004_xxxx.py here...also when I go production, that's just another extra step for python to run.
After I removed those two files, I did the migration which creates another 0003_xxxx.py but when I do migrate it gives me the message of no migrations to apply
I know that by deleting the 0003_xxxx.py and get the original 0003 and 0004 back then do another migration (creates 0005_xxxx.py) then migrate then changes will be made. I know this because I didn't really delete the original 0003 and 0004 I moved it somewhere just in case of this kind of happening.
But why is this though? and is there a way to fix it?
Thanks in advance for any replies
django keep records of current migrations log by table django_migrations in your db.
something like:
The migrations is a chain structure,it's depend on the parent node.By this table django can know which migrations file is executed.
In you case,no migrations to apply because the new create 0003_xxxx.py is record in this table,you can fix it by delete this record in this table.
If you want remove some migration file you need see Squashing migrations.
Or even simply just delete the last migration row from your database you can try the following
First, check which row you want to remove
SELECT * FROM "public"."django_migrations";
Now pick the last id from the above query and execute
DELETE FROM "public"."django_migrations" WHERE "id"=replace with your Id;
run ./manage.py migrate.
just run:
python manage.py run --run-syncdb
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 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.
I'm sorry if the name of the question is misleading but here is the deal.
I have dump of the database that was used with old version of django app (django < 1.7).
I have a new version of code which is using django 1.7. And now I need to upgrade some server with new code while saving all data.
How I thought it would work:
Restore database, run new migration (1.7 migrations) with ./manage.py migrate.
Done!
But when I'm running migrations I have "Relation already exists error".
I know that this is happening probably because database is out of sync with migration history or something... But I don't know what to do.
EDIT1 I sense that the only way is manually create migration script or something, because there is now way to sync database with new migrations now.
So suppose a have a table in database named TABLE and two columns C1 and C2.
Now when I was migrating from 1.6 to 1.7 I've added column C3. So the initial migration looks something like this "create table TABLE with COLUMNS C1, C2, C3".
And when I will try to migrate with old database it wouldn't be ably to do this.
Delete new columns.
Create initial migrations.
Fake initial migrations for all apps:
python manage.py migrate --fake yourappnamehere 0001
Add columns, create new migrations.
Execute new migrations.
I have a Flask project with MySQL database and uses SQLAlchemy as ORM and Flask-Migrate*for migrations.
I wrote my models and when I run the migrations, migration file is empty because existing tables are out of Flask-Migrate's control so I actually have to delete those tables to let migration tool to detect and create them again. But the problem is that I do not want to delete and create my tables.
So is there a way that I can sync my model with my existing table ?
EDIT :
I just found out that in env.py file, it's possible to specify tables that exists
and it will not create those tables :
metadata.reflect(engine, only=["table1", "table2"])
Thanks for the answer.
Automatic migrations are by definition generated as a delta between your models and your database. If you already have a database that was created before you started using Flask-Migrate/Alembic then you can begin tracking migrations from that point on.
If you want to generate an initial migration that takes you to your current version the easiest way is to delete all the tables, as you suggested. To avoid losing your data I can suggest two ideas:
backup your database before deleting your tables, then restore after the migration was generated.
temporarily point your application at an empty database (a different one). Once the migration is generated point it back to your database.
I hope this helps.