I am trying to learn Django by setting up one of the projects I found on github. Afetr I ran the syncdb command it showed
Not synced (use migrations):
- django_extensions
- djangoratings
- profiles
- guardian
(use ./manage.py migrate to migrate these).
When I am running "python manage.py migrate app" , it gives
AttributeError: 'module' object has no attribute 'Migration'.
I also ran schemamigration app --auto and --initial as well. But nothing seems to be working. Can somebody point out where I am going wrong.
Are you actually running python manage.py migrate app exactly? If you want to migrate all apps, just run python manage.py migrate. (without app) If there is an app actually named app that you want to run migrations for, then you would do what you did.
If you still get the error after running python manage.py migrate, then there must be an invalid migration file somewhere. I would migrate each app individually like this:
python manage.py migrate django_extensions
python manage.py migrate djangoratings
... etc.
to find the app with bad a migration file. Once you find the app, look in the migrations folder of the app to find any empty migration files.
First thing you must run initial migration.
python manage.py schemamigration --initial
You are getting this error because of your project structure or there is some issue with your south package .try to remove and reinstalling south.
You need to see that there should not be any extra .py file in your migration folder.
Related
I am having DJango migrations problem while making migration following error is coming.
When I run my applications using python manage.py runserver it shows this :-
However, running python manage.py makemigrations shows no changes detected
And Above three images are result after running python manage.py migrate.
What is the problem with this?
When the *table> already exists Error happens, it is usually due to deleting and rerunning the initial migration or models.py file. For these scenarios,
python manage.py makemigrations <app_name>
python manage.py migrate --fake-initial <app_name>
Or if you want to fake only one migration file
python manage.py migrate <migration_file_number> --fake <app_name>
--fake-initial tells Django to mark initial migration as migrated without actually running its corresponding SQL.
Django's migration document may be helpful
looks like you changed the database or migration files manually.
try to re-create the database.
delete the DB file
delete all migrations files (keep the init file)
run create migrations command
run migrate command
I added some table models in models.py for the first time running the app and then ran python manage.py makemigrations followed by python manage.py migrate. This works well but after adding two more tables it doesn't work again.
It created migrations for the changes made but when I run python manage.py migrate nothing happens. My new tables are not added to the database.
Things I have done:
Deleted all files in migrations folder and then run python manage.py makemigrationsfollowed by python manage.py migrate but the new tables are not still not getting added to the database even though the new table models show in the migration that was created i.e 0001_initial.py.
Deleted the database followed by the steps in 1 above but it still didn't solve my problem. Only the first set of tables get created.
Tried python manage.py makemigrations app_name but it still didn't help.
I have run into this problem before and found that running manage.py for specific tables in this fashion worked:
python manage.py schemamigration mytablename --auto
python manage.py migrate
Also make sure that your new table is listed under INSTALLED_APPS in your settings.py.
Can you post your models?
Have you edited manage.py in any way?
Try deleting the migrations and the database again after ensuring that your models are valid, then run manage.py makemigrations appname and then manage.py migrate.
You could try:
Deleted everything in the django-migrations table.
Deleted all files in migrations folder and then run python manage.py makemigrations followed by python manage.py migrate as you said.
If this doesn't work, try:
Deleted everything in the django-migrations table.
Deleted all files in migrations folder, use your old model.py to run python manage.py makemigrations followed by python manage.py migrate.
Add new model, run python manage.py makemigrations followed by python manage.py migrate again.
I uploaded my modified code with some changes in models. When I run heroku run python manage.py migrate app to apply the database migrations it gave me an error
CommandError: Conflicting migrations detected (0004_auto_20150819_0827, 0008_auto_20150813_1444 in app).
To fix them run 'python manage.py makemigrations --merge'
So when I run heroku run python manage.py makemigrations --merge it gave me output:
Created new merge migration /app/app/migrations/0009_merge.py
Now how can apply this migration to my database ?
Maybe
heroku run python manage.py migrate
or to see what it's going to apply:
heroku run python manage.py showmigrations
Going through Django tutorial 1 using Python 2.7 and can't seem to resolve this error:
OperationalError: no such table: polls_poll
This happens the moment I enter Poll.objects.all() into the shell.
Things I've already tried based on research through the net:
1) Ensured that 'polls' is listed under INSTALLED_APPS in settings.py
Note: I've seen lots of suggestions inserting 'mysite.polls' instead of 'polls' into INSTALLED_APPS but this gives the following error: ImportError: cannot import name 'polls' from 'mysite'
2) Run python manage.py syncdb . This creates my db.sqlite3 file successfully and seemingly without issue in my mysite folder.
3) Finally, when I run python manage.py shell, the shell runs smoothly, however I do get some weird Runtime Warning when it starts and wonder if the polls_poll error is connected:
\django\db\backends\sqlite3\base.py:63: RuntimeWarning: SQLite received a naive datetime (2014-02-03 17:32:24.392000) while time zone support is active.
Any help would be appreciated.
I meet the same problem today and fix it I think you miss some command in tutorial 1 just do follow:
./python manage.py makemigrations polls
python manage.py sql polls
./python manage.py syncdb
then fix it and gain the table polls and you can see the table created you should read the "manage.py makemigrations" command
i figured out the mistake you did, after making changes to your models.py you should run migrate ..i.e
python manage.py migrate
then only your changes(polls_question) will be visible
For those encountering this error in the current django 3.0 release (https://docs.djangoproject.com/en/3.0/intro/tutorial02/), you can rectify it by making sure you've run the following commands in order.
python manage.py makemigrations polls
python manage.py sqlmigrate polls 0001
python manage.py migrate
I've just solved this problem in a very easy and effective way.
This error is coming because of wrong pycache and db.sqlite3 in django
To solve this error we can delete those files and re-make it.
Delete pycache and db.sqlite3 manually.
then run this in terminal:
python manage.py makemigrations polls
After this your database and __pycache will be created again. And just make migrations, it will work fine.
Of course you might want to store your data before deleting database.
I have a Django app that I added South to, performed some migrations with, and runs as expected on my local machine. However, I have had nothing but database errors after pushing my project to Heroku.
In trying to deal with one database error I am experiencing, I attempted a test where I deleted one of my models, pushed the edited models file to Heroku and ran:
heroku run python manage.py schemamigration django_app test_remove_pub --auto
This seemed to work fine. I got back the message:
Running `python manage.py schemamigration apricot_app test_remove_pub --auto` attached
to terminal... up, run.6408
- Deleted model django_app.Publication
- Deleted M2M table for journalists on django_app.Publication
- Deleted M2M table for tags on apricot_app.Publication
Created 0006_test_remove_pub.py. You can now apply this migration with: ./manage.py
migrate django_app
So, South appeared to do everything I expected - it deleted my model and its many to many relationships and made the appropriate migration file. Next, I enter:
heroku run python manage.py migrate django_app
And I get back:
Running `python manage.py migrate django_app` attached to terminal... up, run.4792
Running migrations for django_app:
- Nothing to migrate.
- Loading initial data for django_app.
Installed 0 object(s) from 0 fixture(s)
Why would it say "nothing to migrate" when obviously there are things to migrate??
After you change the models, you need to do :
heroku run python manage.py schemamigration django_app --auto
if anything has changed
and then run
heroku run python manage.py migrate django_app
South applies migrations based on the entries in this database table: south_migrationhistory. So if you want to manually override it,
Remove all the entries with column app_name of the models you changed
Manually remove all the related tables. You can get a list of all the tables by typing this in the django shell:
from django.db.models import get_app, get_models
app = get_app(app_name)
for model in get_models(app, include_auto_created=True):
print model._meta.db_table
Remove the migrations/ folder related to the app
Do a fresh migration: ./manage.py schemamigration app_name --initial
Apply the migration ./manage.py migrate app_name