ProgrammingError: relation "django_session" does not exist - python

Got this error after changing my database from sqlite to postgresql. I've made all my settings changes:
Here's my settings:
DATABASES = {
'default': {
'ENGINE': "django.db.backends.postgresql_psycopg2",
'NAME': "postr1",
'USER': "zorgan",
'PASSWORD': config('DB_PASSWORD'),
'HOST': "localhost",
'PORT': '',
}
}
as well as performing makemigrations and migrations which were all successful. So I'm able to succesfully start my local server:
System check identified no issues (0 silenced).
May 15, 2018 - 08:59:39
Django version 1.11.8, using settings 'draft1.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
however when I go to the site it returns this error:
ProgrammingError at /news/
relation "django_session" does not exist
LINE 1: ...ession_data", "django_session"."expire_date" FROM "django_se...
Any idea what the problem is?

Try fake migrate to zero.
Your migration history shows that sessions table was already made, but you don't have real table.
so following below
python manage.py migrate --fake sessions zero
# then your sessions migrate will be
python manage.py showmigrations
sessions
[ ] 0001_initial
# then migrate with --fake-initial again
python manage.py migrate --fake-initial
Then try again.

I'm using django-v-3
Here is the answer how to solve this issue?
1. python manage.py migrate --fake
2. python manage.py migrate --fake-initial
3. Then write python manage.py runserver
Enjoy
If facing issue use python manage.py help. I hope that you will get the solution.

Related

heroku no such table: auth_user error when trying to run create superuser

I'm trying to create my django app w/ Heroku however I'm stuck on this error. I have seen some information on heroku not being able to use sqllite so I've been trying to update to postgres but not sure why that's not reflected here.
sqlite3.OperationalError: no such table: auth_user
heroku run python3 manage.py createsuperuser
› Warning: heroku update available from 7.41.0 to 7.60.1.
Running python3 manage.py createsuperuser on ⬢ generic-heroku-app... up, run.5883 (Free)
On development database
You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 423, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: auth_user
DB from settings.py
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=600)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '**',
'USER': '**',
'PASSWORD': '**',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
Edit: this thread shows some solid info but I still can't seem to make it work with this info. Not sure where I'm going wrong.. "no such table" error on Heroku after django syncdb passed
Any help would be greatly appreciated!
If your project is connected with github deploy, then in your project, in Procfile, add:
release: python manage.py migrate
web: gunicorn myproject.wsgi

Django py manage.py makemigrate

What can I make with it? I'm beginner in python and django. I download it and I i wrote py manage.py makemigrate and I've get error. Can u help me?
Your issue is with your DB configuration in the setting.py. If you are using the default SQLite then copy/paste this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
and your project will work just fine. After this, run
python manage.py makemigrations
python manage.py migrate #copy all migrations to the database
python manage.py createsuperuser #to have a admin user to login to adminpanel
python manage.py runserver #starting the server
Otherwise, take a look at the official documentation how to connect MySQL, PostgreSQL, Oracle databases and required configurations.
Your error is in here:
SQLite is not like MySQL or other databases. Actually, it is not a real database. You are using a port, username, password and etc. These are the cause of the error. SQLite is not running in the server or another place. It is just a single file contains data information. Update yours to mine above and it should start work again or change your database to MySQL or others.
You need to supply all environment variables that are listed in your settings file. Such as DB_NAME that presented in your screenshot. Search for os.environ[<VARIABLE_NAME>], every VARIABLE_NAME should be defined.
If you are a beginner it is better to stay with the documentation and do like https://docs.djangoproject.com/en/2.1/intro/tutorial01/
If you could share the DB part of the settings.py it would help.
Generally python manage.py startapp appname should create the necessary files for you.
After which a python manage.py makemigrations and python manage.py migrate should work properly. And this should not come.

Changing Django development Database from the default SQLite to PostgreSQL

What are the steps I need to take to migrate from the default SQLite database to Postgres database?
I'm doing this to get my local development environment as close to my live server (which uses postrgres).
Or is there a reason why local development uses SQLite? Is it not recommended to use Postgres for local development?
You can try the following steps:
1. Install psycopg2 to configure the database:
pip install psycopg2
2. Inside the default settings.py
Change original values:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
To:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'NAME_OF_DB',
'USER': 'DB_USER_NAME',
'PASSWORD': 'DB_PASSWORD',
'HOST': 'localhost',
'PORT': 'PORT_NUMBER',
}
}
3. Migrate the DB:
python manage.py makemigrations
python manage.py migrate
EDIT:
Thanks #robotHamster comment. Here is the method to sync the existing data:
Backup the data first:
python manage.py dumpdata > datadump.json
After changing the DB setting:
python manage.py loaddata datadump.json
Source: What's the best way to migrate a Django DB from SQLite to MySQL?
when you are changing the database you might get a UNICODEERRO:'utf-8'
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
After wasting more than 5 days I finally got the solution .. you will never got that accurate error in internet, I figured it out by myself .
python manage.py dumpdata > datadump.json
then change the database settings.py as per your desired database and then apply the following commands ...
python manage.py makemigrations
python manage.py migrate
python manage.py loaddata datadump.json
and then if u got the error I have mentioned earlier, please follow step by step guide :
1.Install notepad ++
2.open your datadum.json file in notepad++
3.on the bottom right corner you will get the encoding will be anything else than utf-8
4.on the top bar select encoding to UTF-8
you are good to go ..then again
python manage.py load data datadump.json
I have suffered a lot for this ...so please upvote, and shares are also appreciated.
Thank you!
and for more clearance, you can watch this video:https://youtu.be/RBtEr3TXNwg
Here is a great tutorial on how to do this from Django Girls
It shows you the installation as well as the required changes in settings.py.
Hope I am not late. So to my experience if you already have data in your sqlite db, you might face some challenges because some fields in sqlite don't directly match with fields in postgres. For example datetime, and boolean fields.
I found a library that helped me to do this:
https://github.com/Hitman23/pgloader
The library does any needed conversions.

Django migration relation does not exist

So I'm trying to run the initial migrations on a django app and when I try to run the migrate command (python manage.py migrate or makemigrations) I get the following error:
psycopg2.ProgrammingError: relation "dotworks_server_internship" does not exist
LINE 1: ...s", "dotworks_server_internship"."questions" FROM "dotworks_...
^
I'm on a Windows environment using Django 1.9.6 and my database is postgres. Plus, I'm using PGAdmin to manage my database.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'dotworks',
'USER': 'postgres',
'PASSWORD': 'mypasswordgoeshere',
'HOST': 'localhost',
'PORT': '5432',
}
}
I had this problem and I had to comment out everything in urls.py that referenced views.py, then run makemigrations. Hope this helps.
Make sure that you don't have any class variables in your code that are calling Django manager
For example:
class SomeViewSet(viewsets.ViewSet):
se = SomeEntity.objects.first() # fetching some entity on the class level
def list(self, request):
# the rest of the code
So, when you try to create/apply migrations, this variable will also try to initialise, and will try to access SomeEntity, but at that moment that entity doesn't even exist, and the error occurs.
If all other solutions mentioned fail, if you are still in development probably the easiest solution is dropping the database (in pgAdmin 4 2.0, right-click on database) and then run makemigrations and migrate.
Try to migrate particular app using following process. Refer Django migrations
python manage.py makemigrations
Initial migration created then run migrate command with app name
python manage.py migrate appname1, appname2
When you run a query before applying migrations this error appears.
If you got this error during python manage.py makemigrations or python manage.py migrate you must consider that makemigrations and migrate commands run after successful django bootstrap! So this error happens when you run a query during django bootstrap! So you must find the place you run this query during bootstrap progress.
For example, during bootstrap, django reads root {project}/urls.py and its nested imports. If you use views or viewsets in urls.py and they are run a query during initializing (in their __init__ method or __init__.pyfile or somewhere etc.), it happens!
In this situation and similars, you must comment out any entry in urls.py and similar files which cause running a query during bootstrap and prevent them from running by raising of exception during bootstrap! makemigrations and migrate need successful bootstrap to be run!
If your commented out code needs to makemigrations and migrate handcooks :D, it needs to be patient and be silent for a cycle or a while ;), and after a successful migrations it could be active and verbose ;D.
Your app is trying to call some DB entries that does not exist.
If you are trying to migrate it to a new database, one of your options is to export a dump of old database and import it to your new DB.
For example in PostgreSQL, import the database using below command then migration will work!
sudo -u postgres -i psql mydb < mydb-export.sql
If you're running in local,
For each Django app (maybe you have only one), erase the content of the migrations folder.
Then, run python manage.py makemigrations app1 app2 app3 (if you have 3 Django apps named app1, app2, app3). This will (re)create the migrations files required to migrate your database
Then, run python manage.py migrate. It will apply the migration files you just created.
This error may have related to previous database error.so if you created new database and you also face that type of error ,you can simply run the command with the app name:
1)python manage.py makemigrations <"app name">
2)python manage.py migrate <"app name">
I've solved this error with this solution.
first remove all url in urls.py .
create simple function view for viewing nothing.
def simple(request):
context = {}
return render(request, 'base.html', context)
and add url to urs.py
do migrate
python manage.py migrate
after migrate,
recover the deleted urls.py contents
:)
For me the error came from some initialization code I put into the app.ready() method. Commenting that part of code allowed me to run the command makemigrations without any issue.
I believe app.ready is called at some point by manage.py even for the makemigrations command, which caused my code to query my database before any migration.
I found the problematic code thanks to the traceback.

Django manage.py Unknown command: 'syncdb'

I'm trying to follow this tutorial but I'm stuck on the 5th step.
When I execute
[~/Django Projects/netmag$] python manage.py syncdb
I get the following error message :
Unknown command: 'syncdb'
Type 'manage.py help' for usage.
and here is the output of ./manage.py help does not contain syncdb command. How do I add it?
Thanks for any help!
Edit :
When I run migrate, I get this error :
"Error creating new content types. Please make sure contenttypes "
RuntimeError: Error creating new content types. Please make sure
contenttypes is migrated before trying to migrate apps individually.
in settings.py :
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admindocs',
'blog',
]
Edit 2:
If I remove 'blog', from settings.py :
:~/Django Projects/netmag$ python manage.py migrate blog
CommandError: App 'blog' does not have migrations.
:~/Django Projects/netmag$ python manage.py makemigrations blog
App 'blog' could not be found. Is it in INSTALLED_APPS?
syncdb command is deprecated in django 1.7. Use the python manage.py migrate instead.
You have to use python manage.py migrate instead rather than python manage.py syncdb
Run python manage.py makemigrations result below
Migrations for 'blog':
blog/migrations/0001_initial.py:
- Create model Blog
and after that run python manage.py migrate result below
Operations to perform:
Apply all migrations: admin, blog, auth, contenttypes, sessions
Running migrations:
Applying article.0001_initial... OK
the actual command is :
python manage.py migrate --run-syncdb
It will solve many errors in django like , Operational error ,No Table found in databse etc.
You can do it like this in stages, let's say you have an app called "example":
Run python manage.py makemigrations example
A number generates like '0001' get the number
Run python manage.py sqlmigrate example 0001, using the number. Check out the scripts.
Run python manage.py migrate example 0001
You can also look at all your migrations like this: python manage.py showmigrations.
If you don't want to commit it, go to the folder and move it somewhere or delete it before doing step 4.
However, there is another error that can be happened as strict mode needs to be enabled for MariaDB.
Keep Database connection in the settings.py file as follows:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test',
'USER': 'root',
'PASSWORD': '',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '3306',
'OPTIONS': {
'sql_mode': 'traditional',
}
}
}
keep in mind about the below code:
'OPTIONS': {
'sql_mode': 'traditional',
}
After all, if your DJango version is backdated, "python manage.py syncdb" will work but for an updated version more than or equal to 1.7, please use "python manage.py migrate"
Thanks

Categories

Resources