I was trying to change the order of the table columns in a postgreSQL table. As I am just starting, it seemed easier to manage.py flush and create a new DB with new name and apply migrations.
I can see the new DB in pgAdmin received all the Django models migrations except my app model/table. I deleted all migrations folder (except 0001_initial.py) and still, when I run python manage.py makemigrations I get:
No changes detected
But the model class table is not in the DB. When I try to migrate the app model it says:
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
No migrations to apply.
Is there any way to delete all tables, makemigrations and migrate and postgres get all the tables? Any idea why postgreSQL/pgAdmin cannot get my Django app model/table?
You can do a particular app migration using below commands:
python manage.py makemigrations your_app_name
python manage.py migrate your_app_name
This works for me.
The app specific migrations in my case were not working.
python manage.py makemigrations your_app_name
python manage.py migrate your_app_name
What did the trick is to modify the models adding an additional fake variable, then all the variables (and not only the additional one) gets picked up by the makemigrations and migrate command. It is not a clever solution but it did the trick.
I am using Django 3.0.2 that is connected to a local MySQL database.My project current has 2 apps, 'accounts' and 'home'.
I dropped all the tables in my database after updating some of my model fields and I removed all the files in the migrations folder except __init__.py. Trying to start the dev server shows an error: Dependency on app with no migrations: accounts, so I ran python manage.py makemigrations which returned this
Migrations for 'accounts':
accounts\migrations\0001_initial.py
- Create model User
Migrations for 'home':
home\migrations\0001_initial.py
- Create model Idea
- Create model Reply
After this, running the server gives a warning saying
You have 1 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): accounts.
Run 'python manage.py migrate' to apply them.
Doing so gives me an InconsistentMigrationHistory exception:
Migration admin.0001_initial is applied before its dependency accounts.0001_initial on database 'default'
Running python manage.py showmigrations returns this:
accounts
[ ] 0001_initial
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
[X] 0003_logentry_add_action_flag_choices
auth
[X] 0001_initial
[X] 0002_alter_permission_name_max_length
[X] 0003_alter_user_email_max_length
[X] 0004_alter_user_username_opts
[X] 0005_alter_user_last_login_null
[X] 0006_require_contenttypes_0002
[X] 0007_alter_validators_add_error_messages
[X] 0008_alter_user_username_max_length
[X] 0009_alter_user_last_name_max_length
[X] 0010_alter_group_name_max_length
[X] 0011_update_proxy_permissions
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
home
[X] 0001_initial
sessions
[X] 0001_initial
I've tried runing the commands in different orders but it eventually comes to the same result. I can provide more details about the app if needed.
I hope you haven't cleared your model class. You can delete all files under migrations directory of your app directory excluding init.py file.
You have to Drop table django_migrations.
After you delete then run
python manage.py makemigrations account
python manage.py makemigrations home
python manage.py migrate
I am a beginner at the django and module and web applications.
Here is the traceback and the data:
OperationalError at /admin/learning_logs/example/
no such column: learning_logs_example.entry_id
Request Method: GET
Request URL: http://localhost:8000/admin/learning_logs/example/
Django Version: 1.11.7
Exception Type: OperationalError
Exception Value:
no such column: learning_logs_example.entry_id
Exception Location: C:\Users\Bryan\Desktop\LEARNI~1\ll_env\lib\site-
packages\django\db\backends\sqlite3\base.py in execute, line 328
Python Executable: C:\Users\Bryan\Desktop\LEARNI~1\ll_env\Scripts\python.exe
Python Version: 3.6.2
Python Path:
['C:\\Users\\Bryan\\Desktop\\learning log',
'C:\\Users\\Bryan\\Desktop\\LEARNI~1\\ll_env\\Scripts\\python36.zip',
'C:\\Users\\Bryan\\AppData\\Local\\Programs\\Python\\Python36-32\\DLLs',
'C:\\Users\\Bryan\\AppData\\Local\\Programs\\Python\\Python36-32\\lib',
'C:\\Users\\Bryan\\AppData\\Local\\Programs\\Python\\Python36-32',
'C:\\Users\\Bryan\\Desktop\\LEARNI~1\\ll_env',
'C:\\Users\\Bryan\\Desktop\\LEARNI~1\\ll_env\\lib\\site-packages']
Server time: Sat, 18 Nov 2017 18:36:14 +0000
For some reason, when I click that link that says "Examples" below,
it says this
I tried migrating but it says
(venv) C:\...\learning log>python manage.py makemigrations
No changes detected
(venv) C:\...\learning log>python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
No migrations to apply.
How did I get it to do that?
Well first I added the example entry/form with a TextField and then I added a "example". I added a ForeignKey object with the parameter as Entry.
I went back to edit my "example" but then the exception showed up.
EDIT:
Well janos wanted the output when I run python manage.py showmigrations learning_logs so here it is:
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
auth
[X] 001_initial
[X] 0001_initial
[X] 0002_alter_permission_name_max_length
[X] 0003_alter_user_email_max_length
[X] 0004_alter_user_username_opts
[X] 0005_alter_user_last_login_null
[X] 0006_require_contenttypes_0002
[X] 0007_alter_validators_add_error_messages
[X] 0008_alter_user_username_max_length
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
learning_logs
[X] 0001_initial
sessions
[X] 0001_initial
Well I hope that helped.
I recreated the exception page right here
As the error message says,
the column entry_id in the table learning_logs_example does not exist.
This is not normal.
If schema migrations were created correctly and applied correctly,
then this should not happen.
Moreover, looking at this:
(venv) C:\...\learning log>python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
No migrations to apply.
The Apply all migrations line should contain the name of your app.
The existing names are standard Django components.
The name of your app is probably learning_log or similar.
In short,
it seems that migrations don't exist for your app.
To debug this,
first of all check the output of showmigrations for your app:
./manage.py showmigrations learning_log
If you don't get an error, then please add the output to your question.
If you get an error CommandError: No migrations present for: ...,
then create the migrations:
./manage.py makemigrations learning_log
And then check the output of showmigrations again,
and please add the output to your question.
If the output looks something like this:
learning_log
[X] 0001_initial
...
That means the database has some records about applied migrations,
and in fact Django thinks that the migrations are correctly applied.
(When in fact they are not, based on your main error.)
With the information I have so far,
it seems your database got out of sync with the model:
some fields have been added to the model without applying to the database,
and the migrations were not correctly saved.
One simple solution could be to manually add the missing columns.
You can find the correct schema by looking at the migration file:
./manage.py sqlmigrate learning_log 0001
From this you should be able to find how the missing entry_id field is declared,
and then manually add it to the table with a query ALTER TABLE learning_logs_example ADD COLUMN entry_id ...,
where the rest of the parameters depend on the definition of the column in the output of sqlmigrate.
If the problem is only one or a few missing fields,
then this workaround may be enough to have something working.
(If not, then jump to the next section.)
You should not stop there though,
because the model may need some indexes as well.
You could either study further output of sqlmigrate and recreate all other objects that depend on learning_logs_example,
such as indexes, triggers.
But a cleaner solution will be to recreate all the tables of the app,
following these steps:
Dump the current data: ./manage.py dumpdata learning_log > learning_log.json
Drop all tables of the app
Recreate the tables of the app: ./manage.py migrate --fake learning_log zero; ./manage.py migrate learning_log
Restore the data: ./manage.py loaddata learning_log
If the workaround of adding columns manually doesn't work
(you stumble into more complicated problems),
then you have two choices:
If you cannot afford to lose data, well then you will have to track down and solve every single problem manually. After that's done, you should dump and restore the tables of the app as I explained in the previous section
If it's ok to lose data, then you can follow the procedure in the previous section, without the dumping and restoring steps. That is, drop and recreate the tables of the app
Finally,
to avoid further problems,
you need to make sure that all migrations are in sync with the model,
and are correctly added to version control.
A good way to test that is install the Django project in a different folder on your computer.
If that doesn't go smoothly,
then the setup is still not good.
Let me know if any of the above needs more clarification.
Is this the first time you are creating migrations for your application? If so, you need to specify the app name to make the migrations:
python manage.py makemigrations my-app
If data isn't matter, you can just drop your database file from here:
C:\Users\bryan_8nva8ki\Desktop\learning logs\db.sqlite3
(I saw that path from your logs)
Then you should make migrate again. Hope it will helps!
I have created a model and when I try to create the table via migration using python manage.py migrate app_name and in response I get
- Nothing to migrate.
- Loading initial data for app_name.
Installed 3 object(s) from 1 fixture(s)
But the table is not created , its a simple process but I don't know why the table is not created. The db used is postgresql and I have also used syncdb but doesn't work .
It seems you haven't created any migrations. You need to create them, before they can be run.
For app's initial first migration you run:
./manage.py schemamigration your_app --initial
For following migrations you run (provided you want to create an automatic migration):
./manage.py schemamigration your_app --auto
You can see more examples in the tutorial.
I'm having trouble with Django south migrations. It may be related to how we've laid our project out but it was working previously, and it works fine locally.
I pushed new code last night that contained a migration in my_app app. So in my local environment...
$ ./manage.py migrate --list
socialaccount
(*) 0001_initial
(*) 0002_genericmodels
(*) 0003_auto__add_unique_socialaccount_uid_provider
(*) 0004_add_sites
(*) 0005_set_sites
(*) 0006_auto__del_field_socialapp_site
(*) 0007_auto__add_field_socialapp_client_id
(*) 0008_client_id
(*) 0009_auto__add_field_socialtoken_expires_at
(*) 0010_auto__chg_field_socialtoken_token
(*) 0011_auto__chg_field_socialtoken_token
payments
(*) 0001_initial
users
(*) 0001_initial
my_app
(*) 0001_initial
(*) 0002_auto__add_organizerapplication
In heroku, it doesn't recognize my_app as an app with migrations. When I attempt to migrate that app....
$ heroku run my_app/manage.py migrate my_app --app=my_app
Running my_app/manage.py migrate my_app attached to terminal... up, run.5016
The app 'my_app' does not appear to use migrations.
./manage.py migrate [appname] [migrationname|zero] [--all] [--list] [--skip] [--merge] [--no-initial-data] [--fake] [--db-dry-run] [--database=dbalias]
If I list the migrations, you'll notice my_app isn't listed...
$ heroku run my_app/manage.py migrate --list --app=my_app
Running my_app/manage.py migrate --list attached to terminal... up, run.8264
socialaccount
(*) 0001_initial
(*) 0002_genericmodels
(*) 0003_auto__add_unique_socialaccount_uid_provider
(*) 0004_add_sites
(*) 0005_set_sites
(*) 0006_auto__del_field_socialapp_site
(*) 0007_auto__add_field_socialapp_client_id
(*) 0008_client_id
(*) 0009_auto__add_field_socialtoken_expires_at
(*) 0010_auto__chg_field_socialtoken_token
(*) 0011_auto__chg_field_socialtoken_token
payments
(*) 0001_initial
users
(*) 0001_initial
I'm not sure it's relevant but my_app is listed in my INSTALLED_APPS when I check.
UPDATE
The issue was that this particular migrations dir was missing __init__.py. Running commands like convert_to_south through Heroku don't impact this as local file changes don't stick. Pushing the repo again with that file got the migrations recognized. I then just had to fake the first migration and I was good.
Make sure you have a init.py file in the migrations folder of the app you want to migrate. South will work locally, but not in production on heroku. Simply copy an init.py file from one of your apps, and put it into the migrations folder for the app you are getting the error for. Push that change to production, and then migrate.