Skip to content
64 changes: 64 additions & 0 deletions specifyweb/specify/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -6977,4 +6977,68 @@ class Migration(migrations.Migration):
model_name='accession',
index=models.Index(fields=['dateaccessioned'], name='AccessionDateIDX'),
),
migrations.CreateModel(
name='AutonumschColl',
fields=[
('collection', models.ForeignKey(db_column='CollectionID', on_delete=django.db.models.deletion.DO_NOTHING, to='specify.collection')),
('autonumberingscheme', models.ForeignKey(db_column='AutoNumberingSchemeID', on_delete=django.db.models.deletion.DO_NOTHING, to='specify.autonumberingscheme')),
],
options={
'db_table': 'autonumsch_coll',
'ordering': (),
'unique_together': {('collection', 'autonumberingscheme')},
'indexes': [
models.Index(fields=['autonumberingscheme'], name='FK46F04F2AFE55DD76'),
models.Index(fields=['collection'], name='FK46F04F2A8C2288BA'),
],
},
),
migrations.CreateModel(
name='AutonumschDiv',
fields=[
('division', models.ForeignKey(db_column='DivisionID', on_delete=django.db.models.deletion.DO_NOTHING, to='specify.division')),
('autonumberingscheme', models.ForeignKey(db_column='AutoNumberingSchemeID', on_delete=django.db.models.deletion.DO_NOTHING, to='specify.autonumberingscheme')),
],
options={
'db_table': 'autonumsch_div',
'ordering': (),
'unique_together': {('division', 'autonumberingscheme')},
'indexes': [
models.Index(fields=['autonumberingscheme'], name='FKA8BE493FE55DD76'),
models.Index(fields=['division'], name='FKA8BE49397C961D8'),
],
},
),
migrations.CreateModel(
name='AutonumschDsp',
fields=[
('discipline', models.ForeignKey(db_column='DisciplineID', on_delete=django.db.models.deletion.DO_NOTHING, to='specify.discipline')),
('autonumberingscheme', models.ForeignKey(db_column='AutoNumberingSchemeID', on_delete=django.db.models.deletion.DO_NOTHING, to='specify.autonumberingscheme')),
],
options={
'db_table': 'autonumsch_dsp',
'ordering': (),
'unique_together': {('discipline', 'autonumberingscheme')},
'indexes': [
models.Index(fields=['autonumberingscheme'], name='FKA8BE5C3FE55DD76'),
models.Index(fields=['discipline'], name='FKA8BE5C34CE675DE'),
],
},
),
migrations.CreateModel(
name='SpecifyuserSpprincipal',
fields=[
('specifyuser', models.ForeignKey(db_column='SpecifyUserID', on_delete=django.db.models.deletion.DO_NOTHING, to='specify.specifyuser')),
('spprincipal', models.ForeignKey(db_column='SpPrincipalID', on_delete=django.db.models.deletion.DO_NOTHING, to='specify.spprincipal')),
],
options={
'db_table': 'specifyuser_spprincipal',
'ordering': (),
'unique_together': {('specifyuser', 'spprincipal')},
'indexes': [
models.Index(fields=['specifyuser'], name='FK81E18B5E4BDD9E10'),
models.Index(fields=['spprincipal'], name='FK81E18B5E99A7381A'),
],
},
),
]
51 changes: 50 additions & 1 deletion specifyweb/specify/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7988,4 +7988,53 @@ class Meta:
db_table = 'tectonicunit'
ordering = ()

save = partialmethod(custom_save)
save = partialmethod(custom_save)

class AutonumSchColl(models.Model):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We did have a convention in the past where we expected to have the Django model class names be in a certain case: where the first letter in the class name is capitalized and all other letters are lowercase.

Here are some related commits I could find:

This specific convention was needed when we were dynamically fetching the model from the models class.
Did we do some work to alleviate the need of the convention?

I did a regular expression search of getattr\((.*models) within our Python files and it looks like (for non-tree classes) having them named this way might at least break WorkBench/BatchEdit DataSets the tables are in, as well as queries.
There may be other places which would break when interacting with these classes.

collection = models.ForeignKey("Collection", db_column="CollectionID", on_delete=models.CASCADE)
autonumberingscheme = models.ForeignKey("AutoNumberingScheme", db_column="AutoNumberingSchemeID", on_delete=models.CASCADE)

class Meta:
db_table = "autonumsch_coll"
unique_together = (("collection", "autonumberingscheme"),)
indexes = [
models.Index(fields=["autonumberingscheme"], name="FK46F04F2AFE55DD76"),
models.Index(fields=["collection"], name="FK46F04F2A8C2288BA"),
Comment on lines +8001 to +8002
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Django should actually index ForeignKeys it creates by default, so these explicit indexes shouldn't be needed:

A database index is automatically created on the ForeignKey. You can disable this by setting db_index to False. You may want to avoid the overhead of an index if you are creating a foreign key for consistency rather than joins, or if you will be creating an alternative index like a partial or multiple column index.

https://docs.djangoproject.com/en/4.2/ref/models/fields/#foreignkey

For an example, here's the indexes from the sprole table:

MariaDB [specify]> show indexes from sprole;
+--------+------------+--------------------------------------------------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
| Table  | Non_unique | Key_name                                                     | Seq_in_index | Column_name   | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Ignored |
+--------+------------+--------------------------------------------------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
| sprole |          0 | PRIMARY                                                      |            1 | id            | A         |          10 |     NULL | NULL   |      | BTREE      |         |               | NO      |
| sprole |          1 | sprole_collection_id_4dccb6f9_fk_collection_usergroupscopeid |            1 | collection_id | A         |           5 |     NULL | NULL   |      | BTREE      |         |               | NO      |
+--------+------------+--------------------------------------------------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
2 rows in set (0.055 sec)

collection_id is already indexed, without specifying anything on the model or migration:

collection = models.ForeignKey(Collection, on_delete=models.CASCADE)

('collection', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='specify.Collection')),


Could we remove the indexes declared explicitly on Foreign Keys in this PR?

]

class AutonumSchDiv(models.Model):
division = models.ForeignKey("Division", db_column="DivisionID", on_delete=models.CASCADE)
autonumberingscheme = models.ForeignKey("AutoNumberingScheme", db_column="AutoNumberingSchemeID", on_delete=models.CASCADE)

class Meta:
db_table = "autonumsch_div"
unique_together = (("division", "autonumberingscheme"),)
indexes = [
models.Index(fields=["autonumberingscheme"], name="FKA8BE493FE55DD76"),
models.Index(fields=["division"], name="FKA8BE49397C961D8"),
]

class AutonumSchDsp(models.Model):
discipline = models.ForeignKey("Discipline", db_column="DisciplineID", on_delete=models.CASCADE)
autonumberingscheme = models.ForeignKey("AutoNumberingScheme", db_column="AutoNumberingSchemeID", on_delete=models.CASCADE)

class Meta:
db_table = "autonumsch_dsp"
unique_together = (("discipline", "autonumberingscheme"),)
indexes = [
models.Index(fields=["autonumberingscheme"], name="FKA8BE5C3FE55DD76"),
models.Index(fields=["discipline"], name="FKA8BE5C34CE675DE"),
]

class SpecifyuserSpprincipal(models.Model):
specifyuser = models.ForeignKey('SpecifyUser', db_column='SpecifyUserID', on_delete=models.deletion.DO_NOTHING)
spprincipal = models.ForeignKey('SpPrincipal', db_column='SpPrincipalID', on_delete=models.deletion.DO_NOTHING)
Comment on lines +8030 to +8031
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is still the DO_NOTHING on_delete behavior here 👀


class Meta:
db_table = 'specifyuser_spprincipal'
ordering = ()
unique_together = (('specifyuser', 'spprincipal'),)
indexes = [
models.Index(fields=['specifyuser'], name='FK81E18B5E4BDD9E10'),
models.Index(fields=['spprincipal'], name='FK81E18B5E99A7381A'),
]