0

I have default schema for my API, and existing schema for data search. This is db settings:

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "OPTIONS": {"options": "-c search_path=public"},
        "NAME": config("DB_NAME", default=""),
        "USER": config("DB_USER_NAME", default=""),
        "PASSWORD": config("DB_PASSWORD", default=""),
        "HOST": config("DB_HOST", default="db"),
        "PORT": config("DB_PORT", default=""),
    },
    "data": {
        "ENGINE": "django.db.backends.postgresql",
        "OPTIONS": {"options": "-c search_path=data"},
        "NAME": config("DB_NAME", default=""),
        "USER": config("DB_USER_NAME", default=""),
        "PASSWORD": config("DB_PASSWORD", default=""),
        "HOST": config("DB_HOST", default="db"),
        "PORT": config("DB_PORT", default=""),
    },
}

For "data" I used the comand python manage.py inspectdb --database=data > apps/data/models.py

And got models.py like this:

class Product(models.Model):
    id = models.AutoField(primary_key=True)
    ...

    class Meta:
        managed = False
        db_table = "Product"

I've tried create some pytest:

@pytest.mark.django_db(databases=["default", "data"], transaction=True)
class TestSomeTestAPIView:
    view_name = "some-view-name"

    def test_some_test_name(self, auth_client):
        print(Product.objects.using("data").all())

And got the error

"Product" table does not exists

So I need use existing db schema "data" without creating migrations, and use default schema as usual.

I've tried https://pytest-django.readthedocs.io/en/latest/database.html but it does not work in my case. Any ideas how to run pytests?

1
  • Please post the actual error without editing it. Commented Jun 7, 2024 at 5:53

1 Answer 1

0

Using your "production" db in tests directly is a bad idea.

That's why Django will create separate test databases for you. To populate the test database, dump your prod data into a fixture and then load that fixture in the test:

If you insist on using the existing database directly, there's a section about how to do it with pytest: https://pytest-django.readthedocs.io/en/latest/database.html#using-an-existing-external-database-for-tests

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.