Search code examples
djangopostgresqlrdbmsordbms

where should data integrity be maintained?


The simplified scenario first:

  • Merchants have products

  • Merchants have categories

  • Products have subcategories

  • A merchant's products' categories must have the merchant's category as the parent category

This is modelled using the RDBMS approach. I'm using Postgres for my database and Django as the web framework. We have features such as enabling/disabling a product/category/merchant. The effects of these must cascade and the data should remain consistent. Where should this be handled ? I can have triggers in my DB to enforce this, or use methods/signals like pre save/post save, pre init/post init in my Django models to do the same. Which is more appropriate. I'm more comfortable with python than SQL, and believe that doing it via Django models makes it more modifiable, since In my company the requirements are prone to frequent and sudden changes.


Solution

  • If it's indeed about data integrity, do it within Postgres, it will be much much easier to stay consistent when your data model expands.

    (I've grown an application from about 10 to about 70 tables, the parts of the data model which where ensured by DB schema have been clean ever since. The parts which tried to maintain a consistency allegedly on a "higher" level screwed up now and then)

    If it's about the behaviour of the application, do it in django.