OperationalError at /admin/learning_logs/example/ - python

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!

Related

Django and pgAdmin not aligned

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.

psycopg2.errors.UndefinedTable: relation "django_admin_log" does not exist

I just started learning Django, and I'm following a book as guide (the book is from August 2022, so new) and I ran into 2 problems.
The first one was that Python couldn't find the module psycopg2 which I then installed. Now I'm a little further and created my first model and migrated it to the database, which all seemed to work well. I then created a superuser and opened localhost:8000/admin/ and it sent me to the admin site, I logged in with my newly created user, so far so good. Now the problem.
This is what the site shows me:
And this is what the log says:
I've tried many approaches I found on here, for example deleted the migrations folder in my applications folder and then migrated my application again. I'll just go through a few other commands I've tried:
>> python manage.py migrate --run-syncdb admin
#CommandError: Can't use run_syncdb with app 'admin' as it has migrations.
>> python manage.py sqlmigrate admin 0001
# response: [The SQL query...]
>> python manage.py syncdb
# response: Unknown command: 'syncdb'
>> python manage.py migrate --fake
#Operations to perform:
# Apply all migrations: admin, auth, blog, contenttypes, sessions
#Running migrations:
# No migrations to apply.
This is what the database looks like right now
I found the answer myself on https://www.pythonanywhere.com/forums/topic/14632/
I first ran the command python manage.py migrate admin zero --fake, and then migrated again with python manage.py migrate, then ran the server and now the error is gone!

Relation does not exist error in Django

I know there are many questions about this problem, I looked through the solutions and unfortunately none of them worked for me.
I created a new app called "usermanagement", and added a model to the app. After adding the model I added usermanagement to INSTALLED_APPS in settings. Then I ran python manage.py makemigrations, and python manage.py migrate. This all worked fine! I also did try running the migrations with the app-name.
The problems start when I try to add a new instance of the model to the database in the Python-Django shell, by using:
>>>a = ClubOfficial(name="randomName", email="randomemail#random.com")
>>>a.save()
I get the following error:
django.db.utils.ProgrammingError: relation
"usermanagement_clubofficial" does not exist
LINE 1: INSERT INTO "usermanagement_clubofficial" ("name", "email") ...
Below is the model code:
class ClubOfficial(models.Model):
name = models.CharField(max_length=254)
email = models.EmailField(max_length=254)
If it helps, I use postgresql, and have tried restarting the server. The other apps in the program also work perfectly fine, it is just usermanagemenet that has this problem.
Does anyone know what could be going wrong?
Thank you for your time!
Note: I created a new app now with a different name, copy-pasted things from usermanagement and everything worked fine. I think the problem might be that before there was an app named usermanagement which was deleted, before I created it again. Maybe that messed up the database somehow.
TL;DR: Make sure your app's migrations folder has an __init__.py file. If it isn't there, create it again as an empty file.
I ran into this. In my case I had a previously working django app, not yet moved to production, so I deleted everything in my app's migrations folder, then using django extensions I wiped the postgresql database and cached files with:
./manage.py clear_cache
./manage.py clean_pyc
./manage.py reset_schema
./manage.py reset_db
# then deleted all files (including __init__.py) from my app's migrations folder.
I verified that my postgresql database had no tables. I then ran:
./manage.py makemigrations
./manage.py migrate
Which gave the output:
No changes detected
./manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
(about 11 more lines of output here which are similar)
It is notable that my model's names where nowhere in the migration. I printed the tables in my postgresql database and the Widget and Section tables were both missing. Running the app gave me this error (I substituted 'app' and 'model' for their real names):
ProgrammingError at /my_path
relation "app_model" does not exist
LINE 1: ..."."my_field", "app_model"."my_field" FROM "appname...
So the tables for the models were not being created in the database. My app's migrations folder was also completely empty. The solution was to just add an empty __init__.py to my app's migrations folder, and after that running makemigrations was able to create and save the migration file in the folder. This could then be executed with migrate. You can test this for yourself by running makemigrations with and without the __init__.py in the migrations/ folder.
This solution was not mine but one given by user Ljubitel on another post but it was not the accepted answer there, but the accepted answer didn't work for me so I wrote this solution here to hopefully help others.
I had this same problem, but all I had to do was run
$ python manage.py makemigrations
and
$ python manage.py migrate
In case you have deleted your migration folder.
Create a folder called migration in whatever app name you have created and then create a file in the migration folder called __init__.py
In my case I was pointing to a different databases between my local server and the production server. The database that the production server was pointing to was a few versions behind, so the server could not locate the relation. If your issue were localized to one environment, check the configs first.
Another method to fix relation does not exist error
Create same table in db(postgre, mysql) using sql query tool
now comment your model in models.py and admin.py
run migration using :
python manage.py makemigrations app_name
python manage.py migrate
now uncomment and run migrations command again
I encountered same issue and fixed using following method, I am using postgres(pgadmin 4).

python manage.py makemigrations gives No changes detected?

I just clone exiting project from github, and dump mysql database in my local machine. Every thing is working fine. I made some changes in myapp/model.py, like add new tables. After that when run
1. python ./manage.py makemigrations myapp. Then it makes migration files like
Migrations for 'myapp':
0001_initial.py:
- Create model AndroidRegkey
- Create model ApkVersion
.......................
.......................
python manage.py migrate myapp it gives following message
Operations to perform:
Apply all migrations: myapp
Running migrations:
No migrations to apply.
This is the first time migration on my local machine. I already have database. But after Adding new models in model.py first time it does not apply any migration to data base, why?
I also go this link stack-over flow but not working.
When I ran python manage.py migrate --list Getting following result.
admin
[X] 0001_initial
auth
[X] 0001_initial
contenttypes
[X] 0001_initial
intracity
(no migrations)
mailer
[X] 0001_initial
[X] 0002_auto_20150720_1433
sessions
[X] 0001_initial
When you apply migrations, the migrations that have been applied are stored in the database. They will also be exported to your database dump. So your database should already be in the correct state after you import the data locally. Django looks at the relevant table, sees you're up to date, and takes no further action.

Django South - Migration not re-creating model after dropping table from sqlite3

I am a newbie and a new user to Django and South. I created a new model (approval.py) in my models package (and linked it in model's init.py) and was able to successfully migrate my app to accept the new model. However, when I tried looking for that model on the admin site, I was getting a 403 forbidden error since the Guardian permissions were not applied.
So I directly dropped the table from sqlite command prompt and tried to perform the following commands again:
$python manage.py syncdb
...
(use ./manage.py migrate to migrate these)
$python manage.py schemamigration myapp --auto
Nothing seems to have changed.
$python manage.py migrate myapp
Running migrations for myapp:
- Nothing to migrate.
- Loading initial data for myapp.
Installed 0 object(s) from 0 fixture(s)
However, the table is no longer getting created, when I check in sqlite command prompt. Also, I had registered this model on my admin.sites.url, but it is no longer displaying it and giving error as:
DatabaseError at /admin/myapp/approval/
no such table: myapp_approval
Request Method: GET
Request URL: http://localhost/admin/approval/
Django Version: 1.5.1
Exception Type: DatabaseError
Exception Value:
no such table: myapp_approval
Exception Location: /home/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py in execute, line 362
Python Executable: /home/bin/python2.7
Python Version: 2.7.5
Python Path:
['.........']
Server time: Wed, 9 Jul 2014 19:12:00 -0500
I have already checked all the other questions posted on StackOverflow about using South for migrations, in particular the error about "Nothing seems to have changed". However I am not able to resolve my issue with any of those solutions.
I kindly request some help in either :
Creating the approval model again from scratch and migrating it successfully
Or Repairing the existing approval model.
Thanks.
Finally after a lot of trial and error, something very simple worked for me. I did not have much luck with the south migrations. So as I had manually dropped the table from sqlite3, I decided to manually create the table in sqlite3 and everything worked perfectly fine.
I used the below script on the sqlite3 prompt and it solved my issue and I was able to access the approval page correctly through my admin site and perform all CRUD operations on it.
CREATE TABLE "myapp_approval" ("id" integer NOT NULL PRIMARY KEY, "desc" varchar(256) NOT NULL);
Thanks everyone for your help and support!
You have already created a migration that creates the new table, and according to South's history management, you've run this migration. South does not know about the actual changes to your database.
What you need to do is to revert to a migration before the new table was created. If you look in your apps folder, you'll find a migrations folder with some migration files in it. If you created the new table in e.g. migration 0005, you need to (optionally) migrate backwards to 0005, and then you need to fake a backwards migration to 0004 and migrate again:
manage.py migrate myapp 0005
manage.py migrate myapp 0004 --fake
manage.py migrate myapp

Categories