I am trying to make a Django app to connect to an existing RDS postgres instance
I have changed my settings.py file to the following:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': <db-name>,
'USER': <username>,
'PASSWORD': <pswd>,
'HOST': 'project-hash.us-east-1.rds.amazonaws.com',
'PORT': '5432',
}
}
After creating Django project, running python manage.py migrate yelds:
Tracking file by folder pattern: migrations
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying auth.0001_initial...Traceback (most recent call last):
File "/home/gustavo.figueiredo/anaconda3/envs/lp_admin/lib/python3.7/site-packages/django/db/backends/utils.py", line 82, in _execute
return self.cursor.execute(sql)
psycopg2.errors.DuplicateTable: relation "auth_permission" already exists
manage.py migrate will always fail if it tries to run a migration and finds that any action it's attempting to perform -- in this case, create a table to store user permission data -- has already been done.
If the database instance already exists, you would be much better served making use of manage.py inspectdb to create models from the database, rather than using manage.py migrate to apply models to the database.
If your database is an existing Django database, and you're looking to just get the migrations up and running, the simplest way is to run python manage.py migrate --fake-initial to mark the migration as having been applied without actually performing any database operations other than bumping the migration version.
psycopg2.errors.DuplicateTable: relation "auth_permission" already exists
You do not have a problem connecting. To get this error, you must already be connected.
The problem is that the migration you are trying to run is incompatible with the database you are connected to.
Related
I'm having a problem with running unit tests in django while using ElephantSQL,
when running command python manage.py runserver everything works just fine,
I'm able to connect to the server without any problem
but when running the command python manage.py test
I'm getting this error below:
C:\Users\Sman9\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\siteid running initialization queries against the production database when it's not needed (for example, when running tests). Djang
warnings.warn(
Got an error creating the test database: permission denied to create database
Creating test database for alias 'default'...
C:\Users\Sman9\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\db\backends\postgresql\base.py:323: RuntimeWarning: Normally Django will use a connection to the 'postgres' database to avoid running initialization queries against the production database when it's not needed (for example, when running
tests). Django was unable to create a connection to the 'postgres' database and will use the first PostgreSQL database instead.
warnings.warn(
Got an error creating the test database: permission denied to create database```
my databases setting in settings.py file
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': '####',
'USER': '#####',
'PASSWORD': '#############',
'HOST': 'chunee.db.elephantsql.com',
'PORT':'',
}
}
}
Make sure that you have granted all required privileges to a user you have created for managing your database.
Using psql you can do so with the following command:
GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;
Additionally, you ought to have psycopg2 installed for the postgres DB to pair with Django.
In a terminal execute the following:
pip install psycopg2
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
I am trying to import an existing database into my Django project, so I run python manage.py migrate --fake-initial, but I get this error:
operations to perform:
Apply all migrations: ExcursionsManagerApp, GeneralApp, InvoicesManagerApp, OperationsManagerApp, PaymentsManagerApp, RatesMan
agerApp, ReportsManagerApp, ReservationsManagerApp, UsersManagerApp, admin, auth, authtoken, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... FAKED
Applying auth.0001_initial... FAKED
Applying contenttypes.0002_remove_content_type_name... OK
Applying GeneralApp.0001_initial...Traceback (most recent call last):
File "/Users/hugovillalobos/Documents/Code/IntellibookWebProject/IntellibookWebVenv/lib/python3.6/site-packages/django/db/back
ends/utils.py", line 83, in _execute
return self.cursor.execute(sql)
psycopg2.ProgrammingError: relation "GeneralApp_airport" already exists
Of course all the tables already exist in the database, that is the reason why I use --fake-initial, that is supposed to fake the creation of database objects.
Why is migrate attempting to create the table GeneralApp__airport instead of faking it?
Just by running the following command would solve the problem:
python manage.py migrate --fake
--fake-initial can't deal with any situation where some of the tables listed in the initial migration exist and some do not. So, if tables exist for some, but not all, of the CreateModel()s in the operations list in your 0001_initial.py, --fake-initial does not apply and it tries to create tables for ALL of the models.
Does that make sense? Not to me...
Steps you can follow to make migrations from an existing database. Firstly empty the django migration table from the database.
delete from django_migrations
Remove migrations from your migrations folder for the app
rm -rf <app>/migrations/
Reset the migration for builtin apps(like admin)
python manage.py migrate --fake
Create initial migration for each and every app
python manage.py makemigrations <app>
The final step is to create fake initial migrations
python manage.py migrate --fake-initial
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.
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.