Open lboc80 opened 6 years ago
Even if you insert a new row with no manager_id
you don't have a path? That's weird…
And yes, you could use regular SQL functions instead of triggers, and call them when you need to. But only the triggers can ensure data integrity.
I do not know if this is the same issue but I also had empty paths when using get_or_create
:
obj, created = Object.objects.get_or_create(slug=slug) # obj is saved under the hood and path is set by triggers
obj.field = value
obj.save() # obj is saved but we have an empty path in Django object, triggers are not run as slug is not modified,
# path is overwritten by Django ORM
I fixed this by adding refresh_from_db
:
obj, created = Object.objects.get_or_create(slug=slug) # obj is saved under the hood and path is set by triggers
obj.refresh_from_db() # read generated path from db into object
obj.field = value
obj.save() # obj is saved but we have correct path now
Another (tested) option is to exclude path from saved fields:
fields = [f.name for f in Object._meta.get_fields() if f.name != 'path' and not f.auto_created and not f.primary_key]
obj.save(update_fields=fields)
This can be put in save()
method for convenience.
hello, thanks for the repo and the example. I tried to adapt it in this way: this is the 'employee' table from a django model:
I also adapted the triggers:
However
path
remains always empty after inserting or updating the entire row or the 'manager_id' column. Am I doing something wrong? I there a way to launch the update functions ath the end of the import process instead of implementing them as triggers?