Django: Force create index on ForeignKey fields when db's `supports_foreign_keys=False`

I'm trying to use Django (v4.1.3) with Planet Scale's DB which is MySQL compliant with the relevent exception of not supporting foreign keys.

For this, they provide a Django DB driver which subs class the MySQL DB feature class

from django.db.backends.mysql.features import \
    DatabaseFeatures as MysqlBaseDatabaseFeatures

class DatabaseFeatures(MysqlBaseDatabaseFeatures):
    supports_foreign_keys = False

Given this, when I generate migrations and apply them on my local MySQL server, I don't see any foreign keys being created as expected. However, I also don't see keys being created but I still want the keys.

For example with the MySQL driver, when I run SHOW CREATE app_user_groups (from django.contrib.auth package) I see the following key:

CREATE TABLE `auth_group_permissions` (
  ...
  KEY `auth_group_permissio_permission_id_84c5c92e_fk_auth_perm` (`permission_id`),
)

But with the Planet Scale driver (supports_foreign_keys = False), Django doesn't create an key/index.

I know in my models, I can explicitly define keys but I don't find this ideal. In addition, I don't have control over models from other Django apps like django.contrib.auth and many other 3rd party ones.

What can I do to make Django create keys on django.db.models.ForeignKey despite support for foreign keys being off.

Back to Top