pytest-django icon indicating copy to clipboard operation
pytest-django copied to clipboard

Create custom types

Open MyGodIsHe opened this issue 10 months ago • 2 comments
trafficstars

Hi All!

I have several databases in my project and one of them is external. I know, it's bad practice but that's how it is. And I need tests for it and pytest-django creates tables from models perfectly, but problems arise with custom types in postgres. I need to insert the creation of types in the interval between the creation of the database and the creation of tables. I found this solution and I don't like it. Is there another way?

@pytest.fixture(scope="session", autouse=True)
def _create_custom_types():
    from django.db.backends.base.schema import BaseDatabaseSchemaEditor

    def create_model(self, model):
        if model == User:
            self.execute("CREATE EXTENSION hstore;")
        orig_create_model(self, model)

    orig_create_model = BaseDatabaseSchemaEditor.create_model
    with patch.object(BaseDatabaseSchemaEditor, "create_model", new=create_model):
        yield

MyGodIsHe avatar Jan 20 '25 10:01 MyGodIsHe