Skip to content

Database Configuration error in update_tree #123

@Nathan-Cohen

Description

@Nathan-Cohen

Hello,

I'm contacting you to report a database configuration problem in the update_tree method of the TreeNodeModel class, which causes the following error:
settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.

The method concerned is as follows:

def update_tree(cls):
        debug_message_prefix = (
            f"[treenode] update {cls.__module__}.{cls.__name__} tree: "
        )

        with debug_performance(debug_message_prefix):
            # update db
            objs_data = cls.__get_nodes_data()

            with transaction.atomic():   # <- Here lies the problem
                obj_manager = cls.objects
                for obj_pk, obj_data in objs_data.items():
                    obj_manager.filter(pk=obj_pk).update(**obj_data)

            # update in-memory instances
            update_refs(cls, objs_data)

            # update cache instances
            update_cache(cls)

I suggest solving this problem by modifying the with transaction.atomic(): block as follows :
with transaction.atomic(using=router.db_for_write(AccessAttempt)):

The final result would be :

def update_tree(cls):
        debug_message_prefix = (
            f"[treenode] update {cls.__module__}.{cls.__name__} tree: "
        )

        with debug_performance(debug_message_prefix):
            # update db
            objs_data = cls.__get_nodes_data()

            with transaction.atomic(using=router.db_for_write(AccessAttempt)): # <- Correction applied here
                obj_manager = cls.objects
                for obj_pk, obj_data in objs_data.items():
                    obj_manager.filter(pk=obj_pk).update(**obj_data)

            # update in-memory instances
            update_refs(cls, objs_data)

            # update cache instances
            update_cache(cls)

Correction Proposal

  • i am willing to create a Pull Request to make this correction if you find it appropriate. I'd be happy to help solve this problem and improve this project.

  • By explicitly specifying using=router.db_for_write(AccessAttempt) when using transaction.atomic(), we make it clear to Django which database router should be used for this particular operation. This can be crucial in environments where there are multiple database configurations, to ensure that write operations are directed to the correct database that supports writing, thus avoiding potential problems associated with attempts to write to a read-only database.

Additional details

Affected class: TreeNodeModel
Relevant method: update_tree
Affected file: /treenode/models.py
I thank you in advance for your time and consideration and remain available to discuss this issue further if needed.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions