Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Database Support (again, but with new databases options from Django 2.2) #838

Closed
Andrew-Chen-Wang opened this issue Apr 30, 2020 · 1 comment

Comments

@Andrew-Chen-Wang
Copy link

I've taken a look at all the issues and #431 before. I'm somewhat new to pytest-django since I mostly used regular unit tests. I noticed the age of that issue, though, and according to https://docs.djangoproject.com/en/3.0/topics/testing/tools/#django.test.TransactionTestCase.databases, it seems like it should be possible now? But how? It just seems confusing how or where I'd set the databases value...

The error I get when running with a PostGIS and a PostgreSQL database (yes, using two different engines) is this:

―――――――――――――――――――――――― ERROR at setup of test_update ―――――――――――――――――――――――――
3373
3374request = <SubRequest '_django_db_marker' for <Function test_update>>
3375
3376    @pytest.fixture(autouse=True)
3377    def _django_db_marker(request):
3378        """Implement the django_db marker, internal to pytest-django.
3379    
3380        This will dynamically request the ``db``, ``transactional_db`` or
3381        ``django_db_reset_sequences`` fixtures as required by the django_db marker.
3382        """
3383        marker = request.node.get_closest_marker("django_db")
3384        if marker:
3385            transaction, reset_sequences = validate_django_db(marker)
3386            if reset_sequences:
3387                request.getfixturevalue("django_db_reset_sequences")
3388            elif transaction:
3389                request.getfixturevalue("transactional_db")
3390            else:
3391>               request.getfixturevalue("db")
3392
3393/usr/local/lib/python3.7/site-packages/pytest_django/plugin.py:499: 
3394_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
3395/usr/local/lib/python3.7/site-packages/pytest_django/fixtures.py:108: in django_db_setup
3396    **setup_databases_args
3397/usr/local/lib/python3.7/site-packages/django/test/utils.py:173: in setup_databases
3398    serialize=connection.settings_dict.get('TEST', {}).get('SERIALIZE', True),
3399/usr/local/lib/python3.7/site-packages/django/db/backends/base/creation.py:72: in create_test_db
3400    run_syncdb=True,
3401/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py:168: in call_command
3402    return command.execute(*args, **defaults)
3403/usr/local/lib/python3.7/site-packages/django/core/management/base.py:369: in execute
3404    output = self.handle(*args, **options)
3405/usr/local/lib/python3.7/site-packages/django/core/management/base.py:83: in wrapped
3406    res = handle_func(*args, **kwargs)
3407/usr/local/lib/python3.7/site-packages/django/core/management/commands/migrate.py:233: in handle
3408    fake_initial=fake_initial,
3409/usr/local/lib/python3.7/site-packages/django/db/migrations/executor.py:117: in migrate
3410    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
3411/usr/local/lib/python3.7/site-packages/django/db/migrations/executor.py:147: in _migrate_all_forwards
3412    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
3413/usr/local/lib/python3.7/site-packages/django/db/migrations/executor.py:245: in apply_migration
3414    state = migration.apply(state, schema_editor)
3415/usr/local/lib/python3.7/site-packages/django/db/migrations/migration.py:124: in apply
3416    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
3417/usr/local/lib/python3.7/site-packages/django/db/migrations/operations/models.py:92: in database_forwards
3418    schema_editor.create_model(model)
3419/usr/local/lib/python3.7/site-packages/django/db/backends/base/schema.py:322: in create_model
3420    sql, params = self.table_sql(model)
3421/usr/local/lib/python3.7/site-packages/django/db/backends/base/schema.py:159: in table_sql
3422    definition, extra_params = self.column_sql(model, field)
3423/usr/local/lib/python3.7/site-packages/django/db/backends/base/schema.py:212: in column_sql
3424    db_params = field.db_parameters(connection=self.connection)
3425/usr/local/lib/python3.7/site-packages/django/db/models/fields/__init__.py:715: in db_parameters
3426    type_string = self.db_type(connection)
3427_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
3428
3429self = <django.contrib.gis.db.models.fields.MultiPolygonField: polygon>
3430connection = <django.db.backends.postgresql.base.DatabaseWrapper object at 0x7f008e3938d0>
3431
3432    def db_type(self, connection):
3433>       return connection.ops.geo_db_type(self)
3434E       AttributeError: 'DatabaseOperations' object has no attribute 'geo_db_type'
3435
3436/usr/local/lib/python3.7/site-packages/django/contrib/gis/db/models/fields.py:105: AttributeError
3437                 

There was also some discussion in Django Dev of this being a syncdb issue for a geo and a non-geo database, but I'm having no trouble writing the code and testing it via the Django views... for now, of course.

I will admit, I haven't tried testing this with normal Django TestCase.

@Andrew-Chen-Wang
Copy link
Author

I've just decided to create a new settings file, set the DATABASES name to just the default, and emptied the database routers list. Finally, I just used a regular TransactionTestCase, another test setting file, to make sure a regular query would go to the correct database (instead of the geo one from the error). Oh well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant