This is a very simple issue but may take a long time resolve if you fail to look at the correct point. Especially there's not much content online to hint the source of the problem.
I have a Django project of version 3.2.5.
I created a new model: app.model. Then I ran python manage.py makemigrations app command.
When I ran the tests via pytest, I got the following errors:
E psycopg2.errors.DuplicateTable: relation "app_model" already exists
E django.db.utils.ProgrammingError: relation "app_model" already exists
However there's no such table neither on my local database nor in test database which is created from scratch.
I pushed my code on a branch to Github and build succeeded online with no problems. However my test suite complained about this existing table. I ran python manage.py migrate app and it worked.
I realized it is a problem caused by using pytest with --reuse-db argument. It doesn't ask you to delete an orphan test database in this case, it reuses it.
Instead of using pytest, when I run python manage.py test, I got the following prompt:
Type 'yes' if you would like to try deleting the test database 'test_project', or 'no' to cancel: Got an error creating the test database: database "test_project" already exists
I typed yes, the orphan test_project database was deleted & re-created from scratch and the errors disappeared.
Another solution would be using --create-db with pytest command or adding this option to the pytest.ini file.
Related
Hi guys so I know this question has been asked many times but the solution that have been provided doesn't seem to fix the source of my problem.
Based on what I've seen the solution is to run create extension hstore.
However, I get the error django.db.utils.ProgrammingError: type "hstore" does not exist when I'm trying to run a django test, ie python manage.py test test_function
I'm not familiar with django tests but from what I'm seeing with the codebase I am working with is that the django test is creating a test database when I run the django test command.
When I checkout what extensions are available with the test database with \dx I see that it does not have hstore. If I run create extension hstore then the hstore extension gets created. Problem is that whenever I rerun the django test, python manage.py test test_function it tells me the test database already exists and I need to delete it first otherwise the command exits. By deleting the database the extension that just got added also gets removed thus when I run python manage.py test test_function again I end up with the same error.
How do I get rid of the error? Thank you.
Turns out what I needed to do was create the hstore extension to the default template1 with:
psql template1 -c 'create extension hstore;'
I have deleted a class from model.py but whenever I run python manage.py migrate, I get this:
intergrityerror : not null constraint failed: appname_modelclassfieldname.user_id
The most challenging thing is that I had already deleted the model class of the related field django is pointing error at.
You have to create a migration before running migrate. As you have not specified your Django version, here is what you need to do for the most recent version (1.11) which also works down to at least 1.9:
$ ./manage.py makemigrations # > creates a migration file
$ ./manage.py migrate
You can specify the the app in the makemigrations call if you want.
Concerning:
The most challenging thing is that i had already deleted the model class of the related field django is pointing error at .
Use a version system for your own health (e.g. GIT) or an editor that supports local versioning (like PyCharm) - best use both - even if you are working alone. (You are never alone, after some weeks, the code looks like it was written by someone else...)
The problem was an error with the server .I had delete the app and create a new one ,and everything is working just fine now
I am running a GIS application while getting this error. I have attached error snapshot . Can Someone guide where is error ? If need to see code . Let me know which file you need.
Especially the first line of your exception value pretty much says it all.
Column users.parent_id does not exist
The application is trying to access parent.id from the users table from your database, which obviously does not exist. With other words, your database is not sync with the model structure in the source code. Probably all you have to do, is to run the migrations to add all the missing structures or changes to your database.
If you've developed some of the database structures yourself, you have to run the makemigrations command to build the new migration set for your database.
./manage.py makemigrations
If you have created new migration files or if you have installed and integrated modules into your app, you have to apply the migrations to your database.
./manage.py migrations
I have an app that I recently did a migration for that included adding a new table. I can see that the migration worked, the table is populated, I can access it, etc. Even when I run
python manage.py migrate my_app -l
I can see the migration as having been successfully run.
However when I run my unit tests I get:
OperationalError: no such table: my_app_my_table
The only thing I can thing of is that the in memory database is not running all the migrations? Or it's not doing it correctly? Any help would be greatly appreciated.
It could be the case that your migrations aren't actually generating the SQL code you intended, and it's probably worth running sqlmigrate on the specific migrations to make sure it's generating code to make the table.
If the SQL code looks right, something weird could have happened and django thinks you ran the migration but it didn't actually change the database. In that case, you could try restoring an older version of the database and running the migrations on that?
I have a django project and I am trying to write some tests for it. However, my initial_data fixtures cause an error when running the test.
The error that I am getting is:
django.db.utils.ProgrammingError: Problem installing fixture 'accounts/fixtures/initial_data.json': Could not load auth.Group(pk=1): relation "auth_group" does not exist
LINE 1: UPDATE "auth_group" SET "name" = '...
If I rename my fixture to something other than initial_data so that it doesn't get loaded by default, it works, but I don't want to rename my fixtures, because that would mean that I can no longer run loaddata without arguments.
I have found this bug, but my project does not have any initial migrations. Also, I have other fixtures which are loaded just fine.
So far, I have tried:
flushing my development database, as well as deleting any possible migration files
deleting and re-creating my virtual env
changing the order of my apps in INSTALLED_APPS
calling the flush command in the .setUp() method.
I should mention that I am using the APITestCase from django-rest-framework.
Any suggestions are welcomed. Thanks.
Ok, so finally, it seems that the problem wasn't just when I was testing. When I changed back to running my server, I noticed I was getting the same error.
Every single similar problem I found had something to do with migrations, but I didn't even had those, because running ./manage.py makemigrations was not generating them.
So I ended up doing ./manage.py makemigrations *app_name* for each of my apps, and everything started working again ...