Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

'relation "django_site" does not exist' only when running Django tests in GitHub Actions

I have a Django 3.1 project with an Admin class like this really simple example:

from django.contrib import admin
from .models import Book

class BookAdmin(admin.ModelAdmin):
   def book_title(self, obj):
       return obj.title
admin.site.register(Book, BookAdmin)

And I test that class:

from django.contrib.admin.sites import AdminSite
from django.test import TestCase

from myapp.admin import BookAdmin
from myapp.models import Book

class BookAdminTestCase(TestCase):
    def test_book_title(self):
        book = Book(title="Hello")
        book_admin = BookAdmin(Book, AdminSite())
        self.assertEqual(book_admin.book_title(book), "Hello")

This works fine when I run tests (./manage.py test) on my local development site.

But when I run tests in a GitHub Action step...

    - name: Run Tests
      run: |
        pipenv run ./manage.py collectstatic --verbosity=0 --noinput
        pipenv run ./manage.py test
      env:
        DJANGO_SETTINGS_MODULE: config.settings.tests
        # etc

...while other tests all work fine, tests using the AdminSite() call fail with:

psycopg2.errors.UndefinedTable: relation "django_site" does not exist
196
LINE 1: ..."django_site"."domain", "django_site"."name" FROM "django_si...

These tests use the same Django settings as when run locally.

If I run migrations in the GitHub Actions step before I run the tests...

    - name: Run Tests
      run: |
        pipenv run ./manage.py collectstatic --verbosity=0 --noinput
        pipenv run ./manage.py migrate --verbosity=0 --noinput
        pipenv run ./manage.py test
      env:
        DJANGO_SETTINGS_MODULE: config.settings.tests
        # etc

...this error doesn't occur, so I can get round this, but it puzzles me.

Shouldn't ./manage.py test run migrations when creating the test database anyway? Given all the other tests succeed before these, I'm stumped as to why only these Site-related tests fail.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...