History for ManyToMany tables not being tracked with django-simple-history

62 Views Asked by At

I have this model:

class UserExtendProduct(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    products = models.ManyToManyField(Product)
    history = HistoricalRecords()

Django created this table to manage this:

mysql> desc userextendproduct_products;
+----------------------+------+------+-----+---------+----------------+
| Field                | Type | Null | Key | Default | Extra          |
+----------------------+------+------+-----+---------+----------------+
| id                   | int  | NO   | PRI | NULL    | auto_increment |
| userextendproduct_id | int  | NO   | MUL | NULL    |                |
| product_id           | int  | NO   | MUL | NULL    |                |
+----------------------+------+------+-----+---------+----------------+

I created this model to represent that table:

class UserExtendProductProducts(models.Model):
    userextendproduct = models.ForeignKey(UserExtendProduct, models.DO_NOTHING)
    product = models.ForeignKey(Product, models.DO_NOTHING)
    history = HistoricalRecords()

    class Meta:
        managed = False
        db_table = 'userextendproduct_products'
        unique_together = ('userextendproduct', 'product')

When userextendproduct_products is updated I would expect historicaluserextendproductproducts to track that update, but it does not.

But here is something odd:I have 2 M2M relations and one is tracking history and one is not. For the one that is working the M2M table is userinfo_institution and the history table is historicaluserinfo_institution. The one that doesn't work (detailed above) the M2M table is userextendproduct_products and the history table is historicaluserextendproductproducts - note the missing underscore.

I have seen some posts here and also on the project's github page that talk about support for this being added. Is there something I am missing to get this working?

1

There are 1 best solutions below

0
Larry Martell On

I figured out the issue. I had to add

history = HistoricalRecords(m2m_fields=[products])

Not sure where that is documented.