Search code examples
python-3.xdjangodjango-4.0social-auth-app-django

NotImplementedError: subclasses of BaseDatabaseIntrospection may require a get_sequences() method


I have recently upgraded the social-auth-app-django library version from 5.0.0 to 5.4.1. I am adding the versions of few of the other libraries used:

Django==4.2.15
django-tenant-schemas==1.12.0
django-restql==0.15.4
djangorestframework==3.15.2
social-auth-app-django==5.4.1
django-postgres-extra==2.0.9rc11

I keep getting the below error after this upgrade, adding the stack trace below:

Applying social_django.0011_alter_id_fields...
Traceback (most recent call last):
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/django/db/models/query.py", line 916, in get_or_create
    return self.get(**kwargs), False
           ^^^^^^^^^^^^^^^^^^
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/django/db/models/query.py", line 637, in get
    raise self.model.DoesNotExist(
core.models.LoyaltyTenant.DoesNotExist: LoyaltyTenant matching query does not exist.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/circleci/bolapi-build/manage.py", line 35, in <module>
    main()
  File "/home/circleci/bolapi-build/manage.py", line 31, in main
    execute_from_command_line(sys.argv)
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/django/core/management/commands/test.py", line 24, in run_from_argv
    super().run_from_argv(argv)
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/django/core/management/base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/django/core/management/base.py", line 458, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/django/core/management/commands/test.py", line 68, in handle
    failures = test_runner.run_tests(test_labels)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/django/test/runner.py", line 1054, in run_tests
    old_config = self.setup_databases(
                 ^^^^^^^^^^^^^^^^^^^^^
  File "/home/circleci/bolapi-build/core/test_runner.py", line 58, in setup_databases
    tenant, _ = get_tenant_model().objects.get_or_create(
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/django/db/models/manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/django/db/models/query.py", line 923, in get_or_create
    return self.create(**params), True
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/django/db/models/query.py", line 658, in create
    obj.save(force_insert=True, using=self.db)
  File "/home/circleci/bolapi-build/core/models.py", line 87, in save
    return super(LoyaltyTenant, self).save(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/tenant_schemas/models.py", line 71, in save
    self.create_schema(check_if_exists=True, verbosity=verbosity)
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/tenant_schemas/models.py", line 115, in create_schema
    call_command('migrate_schemas',
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/django/core/management/__init__.py", line 194, in call_command
    return command.execute(*args, **defaults)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/django/core/management/base.py", line 458, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/tenant_schemas/management/commands/migrate_schemas.py", line 48, in handle
    executor.run_migrations(tenants=tenants)
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/tenant_schemas/migration_executors/base.py", line 61, in run_migrations
    self.run_tenant_migrations(tenants)
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/tenant_schemas/migration_executors/standard.py", line 9, in run_tenant_migrations
    run_migrations(self.args, self.options, self.codename, schema_name)
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/tenant_schemas/migration_executors/base.py", line 31, in run_migrations
    MigrateCommand(stdout=stdout, stderr=stderr).execute(*args, **options)
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/django/core/management/base.py", line 458, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/django/core/management/base.py", line 106, in wrapper
    res = handle_func(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/django/core/management/commands/migrate.py", line 356, in handle
    post_migrate_state = executor.migrate(
                         ^^^^^^^^^^^^^^^^^
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/django/db/migrations/executor.py", line 135, in migrate
    state = self._migrate_all_forwards(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/django/db/migrations/executor.py", line 167, in _migrate_all_forwards
    state = self.apply_migration(
            ^^^^^^^^^^^^^^^^^^^^^
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/django/db/migrations/executor.py", line 252, in apply_migration
    state = migration.apply(state, schema_editor)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/django/db/migrations/migration.py", line 132, in apply
    operation.database_forwards(
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/django/db/migrations/operations/fields.py", line 235, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/psqlextra/backend/schema.py", line 851, in alter_field
    super().alter_field(model, old_field, new_field, strict)
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/django/db/backends/base/schema.py", line 831, in alter_field
    self._alter_field(
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/django/db/backends/postgresql/schema.py", line 288, in _alter_field
    super()._alter_field(
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/django/db/backends/base/schema.py", line 1011, in _alter_field
    fragment, other_actions = self._alter_column_type_sql(
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/django/db/backends/postgresql/schema.py", line 241, in _alter_column_type_sql
    if sequence_name := self._get_sequence_name(table, column):
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/django/db/backends/postgresql/schema.py", line 138, in _get_sequence_name
    for sequence in self.connection.introspection.get_sequences(cursor, table):
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/circleci/.pyenv/versions/3.11.9/lib/python3.11/site-packages/django/db/backends/base/introspection.py", line 161, in get_sequences
    raise NotImplementedError(
NotImplementedError: subclasses of BaseDatabaseIntrospection may require a get_sequences() method

Exited with code exit status 1

Let me know if someone else has faced a similar issue, and if the issue lies with social-auth-app-django package or is a cause of some other dependency.


Solution

  • Ok, my solution was to implement the method in a middleware controller:

    /app/db/backends/postgresql/base.py

    from tenant_schemas.postgresql_backend.base import DatabaseWrapper as TenantDatabaseWrapper
    from tenant_schemas.postgresql_backend.introspection import DatabaseSchemaIntrospection as TenantDatabaseSchemaIntrospection
    
    class DatabaseSchemaIntrospection(TenantDatabaseSchemaIntrospection):
        def get_sequences(self, cursor, table_name, table_fields=()):
            cursor.execute(
                """
                SELECT
                    s.relname AS sequence_name,
                    a.attname AS colname
                FROM
                    pg_class s
                    JOIN pg_depend d ON d.objid = s.oid
                        AND d.classid = 'pg_class'::regclass
                        AND d.refclassid = 'pg_class'::regclass
                    JOIN pg_attribute a ON d.refobjid = a.attrelid
                        AND d.refobjsubid = a.attnum
                    JOIN pg_class tbl ON tbl.oid = d.refobjid
                        AND tbl.relname = %s
                        AND pg_catalog.pg_table_is_visible(tbl.oid)
                WHERE
                    s.relkind = 'S';
            """,
                [table_name],
            )
            return [
                {"name": row[0], "table": table_name, "column": row[1]}
                for row in cursor.fetchall()
            ]
    
    class DatabaseWrapper(TenantDatabaseWrapper):
        def __init__(self, *args, **kwargs):
            super(DatabaseWrapper, self).__init__(*args, **kwargs)
            self.introspection = DatabaseSchemaIntrospection(self)
            self.set_schema_to_public()
    

    The get_sequence method is the same as in .../lib/python3.11/site-packages/django/db/backends/postgresql/introspection.py.

    Then you have to configure the ENGINE in your settings.py:

    DATABASES = {
        'default': {
            # ... other settings
            'ENGINE': 'app.db.backends.postgresql',
            # ... other settings
        }
    }
    

    I don't know why tenant_schemas is not using the correct postgresql classes.