Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why is DSH not using SQL triggers instead of post_save ? #726

Open
elnygren opened this issue Oct 18, 2020 · 1 comment
Open

Why is DSH not using SQL triggers instead of post_save ? #726

elnygren opened this issue Oct 18, 2020 · 1 comment

Comments

@elnygren
Copy link

It seems like django-simple-history is solving the problem of maintaining something commonly known as a "history table". This is usually solved quite elegantly at the SQL level using triggers (https://www.postgresql.org/docs/13/sql-createtrigger.html).

Some notes on why this is a good approach:

  • SQL triggers automatically run inside the transaction where they we're triggered in
  • post_save does not run in the same transaction as Model.save automatically
  • SQL triggers also handle situations where someone bypasses Django ORM or does anything that bypasses post_save
    • bulk_create, bulk_delete, .update etc. are all solved by SQL triggers
    • manual SQL through django or even directly at the DB are also solved by SQL triggers

So it at least seems that SQL triggers are superior and would save a lot of python code here.
Could django-simple-history just generate the correct trigger(s) instead of using post_save ?

@jeking3
Copy link
Contributor

jeking3 commented Sep 16, 2021

Django has some pretty nonsensical transactional behavior; I usually enable global request transactions in my projects. Request processing should succeed or fail, signals and all. So that can mitigate bullet #2; I would suggest if people want atomicity with history they do the same.

As for using the database, is there a solution for every database the library claims to support? What you suggested is database-specific behavior; there is already some in the as_of query processing and it's not awesome to maintain...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants