Importing simple table to Django seems to generate extraneous field

This is my table as defined in MySQL:

mysql> describe rules_enabled;
+-------------------+----------------------------------+------+-----+---------+----------------+
| Field             | Type                             | Null | Key | Default | Extra          |
+-------------------+----------------------------------+------+-----+---------+----------------+
| rule_id           | int                              | NO   | PRI | NULL    | auto_increment |
| rule_name         | varchar(20)                      | NO   | MUL | NULL    |                |
| role_name         | varchar(20)                      | NO   | MUL | NULL    |                |
| role_dependency   | varchar(20)                      | YES  | MUL | NULL    |                |
| range_application | enum('PERIOD','WIDE','REF_DATE') | NO   |     | PERIOD  |                |
| range_duration    | int                              | YES  |     | -1      |                |
| reference_date    | timestamp                        | YES  |     | NULL    |                |
| rule_parameters   | json                             | YES  |     | NULL    |                |
+-------------------+----------------------------------+------+-----+---------+----------------+

and this is how it is understood by Django doing manage.py inspectdb rules_enabled:

class RulesEnabled(models.Model):
    rule_id = models.AutoField(unique=True)
    rule_name = models.CharField(max_length=20, db_comment='Rule name')
    role_name = models.ForeignKey('ShiftRoles', models.DO_NOTHING, db_column='role_name')
    role_dependency = models.ForeignKey('ShiftRoles', models.DO_NOTHING, db_column='role_dependency', related_name='rulesenabled_role_dependency_set', blank=True, null=True)
    range_application = models.CharField(max_length=8, db_comment='Type of range used to evaluae the rule.')
    range_duration = models.IntegerField(blank=True, null=True, db_comment='Extension of the range (WIDE Only) in days.')
    reference_date = models.DateTimeField(blank=True, null=True, db_comment='Reference day (REF_DATE ONLY).')
    rule_parameters = models.JSONField(blank=True, null=True, db_comment='Rule parameters')

    class Meta:
        managed = False
        db_table = 'rules_enabled'
        db_table_comment = 'Rules associated to the defined roles'

problem 1, using it like this in file models.py, running manage.py makemigrations gives:

  File "/usr/local/django_venv/lib/python3.10/site-packages/django/db/models/fields/__init__.py", line 2812, in contribute_to_class 
raise ValueError(
ValueError: Model b2mmsdb.RulesEnabled can't have more than one auto-generated field.

(I think this is because of the Django-created extra id, it doesn’t like the name of my own PK) Next, I change line

    rule_id = models.AutoField(unique=True)

to

    rule_id = models.AutoField(primary_key=True)

Django does allright makemigrations and migrate but now, in the Django shell, I get:

>>> rule_205=RulesEnabled.objects.filter(rule_id=205).get()
>>> print(rule_205.rule_name)
overlap                        <--------------fantastic! it's even right!
>>> RulesEnabled.objects.filter(rule_name='overlap').count()
23                             <------still OK. But, waitaminute.....

>>> print(rule_205.role_name)
Traceback (most recent call last):
  File "/usr/local/django_venv/lib/python3.10/site-packages/django/db/models/fields/related_descriptors.py", line 236, in __get__
    rel_obj = self.field.get_cached_value(instance)
  File "/usr/local/django_venv/lib/python3.10/site-packages/django/db/models/fields/mixins.py", line 15, in get_cached_value
    return instance._state.fields_cache[cache_name]
KeyError: 'role_name'          <---------------- huh?
(....more traceback from Django innards...)
ValueError: Field 'role_id' expected a number but got 'cr_shadow_loc'.

>>> RulesEnabled.objects.filter(role_name='cr_shadow_loc').count()
Traceback (most recent call last):
  File "/usr/local/django_venv/lib/python3.10/site-packages/django/db/models/fields/__init__.py", line 2117, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: 'cr_shadow_loc'
....
ValueError: Field 'role_id' expected a number but got 'cr_shadow_loc'.

Now, where does role_id come from, and why Django seems to believe is a PK? Let’s have a look at all fields:

>>> field_names = [f.name for f in RulesEnabled._meta.get_fields()]
>>> print(field_names)
['rule_id', 'rule_name', 'role_name', 'role_dependency', 'range_application', 'range_duration', 'reference_date', 'rule_parameters']

No role_id that I can see. This is not the first table I import to Django, have seen and fixed other strange facts, but can’t figure out this at all.

Please help, or suggest other debugging shell commands. Thank you!!

Well, it happens that role_id is in another table, and it comes into play because role_name in my malfunctioning table is a FK relation to that. So, I have to add a clause to_field, changing line:

role_name = models.ForeignKey('ShiftRoles', models.DO_NOTHING, db_column='role_name')

to

role_name = models.ForeignKey('ShiftRoles', models.DO_NOTHING, db_column='role_name',to_field='role_name')

As simple as that. Thanks for reading!