AutoSyncRecord inconsistent after file change #9162

Closed
opened 2025-12-29 20:46:23 +01:00 by adam · 4 comments
Owner

Originally created by @alehaa on GitHub (Jan 26, 2024).

Originally assigned to: @jeremystretch on GitHub.

Deployment Type

Self-hosted

NetBox Version

v3.7.1

Python Version

3.11

Steps to Reproduce

  1. Create data source TestData:

    Field Value
    Name TestData
    Type git
    URL https://github.com/netbox-community/netbox-plugin-tutorial.git
    enabled yes
  2. Sync data source TestData.

  3. Create export template TestExport:

    Field Value
    Name TestExport
    Content Types Circuits > Circuit (but any will do)
    Data Source TestData
    Data File README.md
    Auto Sync Enabled yes
  4. Edit export template TestExport.

  5. Select Data file tutorial/step01-initial-setup.md and save. (will fail).

  6. Go back to edit form for export template TestExport.

  7. Set Data source, Data file and Auto Sync enabled to null and save.

Expected Behavior

Step 5: Edit succeeds.
Step 7: AutoSyncRecord is removed from database.

Observed Behavior

Step 5: An IntegrityError error is raised, as SyncedDataMixin's save() method won't change existing AutoSyncRecord objects.

Step 7: Edit succeeds, but the related AutoSyncRecord stays in the database. This is due SyncedDataMixin's save() method will remove related AutoSyncRecord objects, but the QuerySet filter uses fields of the object previously set to None in the edit form.

Originally created by @alehaa on GitHub (Jan 26, 2024). Originally assigned to: @jeremystretch on GitHub. ### Deployment Type Self-hosted ### NetBox Version v3.7.1 ### Python Version 3.11 ### Steps to Reproduce 1. Create data source `TestData`: | Field | Value | |--------|--------| | **Name** | TestData | | **Type** | git | | **URL** | https://github.com/netbox-community/netbox-plugin-tutorial.git | | **enabled** | `yes` | 1. Sync data source `TestData`. 1. Create export template `TestExport`: | Field | Value | |--------|--------| | **Name** | TestExport | | **Content Types** | `Circuits > Circuit` (but any will do) | | **Data Source** | TestData | | **Data File** | README.md | | **Auto Sync Enabled** | `yes` | 1. Edit export template `TestExport`. 1. Select **Data file** `tutorial/step01-initial-setup.md` and save. (**will fail**). 1. Go back to edit form for export template `TestExport`. 1. Set **Data source**, **Data file** and **Auto Sync enabled** to null and save. ### Expected Behavior Step 5: Edit succeeds. Step 7: `AutoSyncRecord` is removed from database. ### Observed Behavior Step 5: An `IntegrityError` error is raised, as `SyncedDataMixin`'s [`save()` method](https://github.com/netbox-community/netbox/blob/b9cac97b73c1c08213db6272de11c03711491486/netbox/netbox/models/features.py#L424) won't change existing `AutoSyncRecord` objects. Step 7: Edit succeeds, but the related `AutoSyncRecord` stays in the database. This is due `SyncedDataMixin`'s [`save()` method](https://github.com/netbox-community/netbox/blob/b9cac97b73c1c08213db6272de11c03711491486/netbox/netbox/models/features.py#L497) will remove related `AutoSyncRecord` objects, but the `QuerySet` filter uses fields of the object previously set to `None` in the edit form.
adam added the type: bugstatus: acceptedseverity: medium labels 2025-12-29 20:46:23 +01:00
adam closed this issue 2025-12-29 20:46:23 +01:00
Author
Owner

@jeremystretch commented on GitHub (Jan 29, 2024):

Please revise your post above to include explicit instructions. It's not clear exactly which actions are needed to reproduce the bug. Providing a concrete example would be very helpful.

@jeremystretch commented on GitHub (Jan 29, 2024): Please revise your post above to include explicit instructions. It's not clear exactly which actions are needed to reproduce the bug. Providing a concrete example would be very helpful.
Author
Owner

@alehaa commented on GitHub (Feb 1, 2024):

@jeremystretch I've updated the instructions to reproduce the issue.

@alehaa commented on GitHub (Feb 1, 2024): @jeremystretch I've updated the instructions to reproduce the issue.
Author
Owner

@alehaa commented on GitHub (Feb 9, 2024):

I believe this could be solved easily by altering the delete mechanism in SyncDataMixin to delete all records that should not be present in the database at the end of save() and delete(). I could check this locally and provide a PR if wanted …

@alehaa commented on GitHub (Feb 9, 2024): I believe this could be solved easily by altering the delete mechanism in `SyncDataMixin` to delete all records that should not be present in the database at the end of `save()` and `delete()`. I could check this locally and provide a PR if wanted …
Author
Owner

@jeremystretch commented on GitHub (Feb 16, 2024):

I believe this could be solved easily by altering the delete mechanism in SyncDataMixin to delete all records that should not be present in the database at the end of save() and delete(). I could check this locally and provide a PR if wanted …

Thanks @alehaa. I think you're on the right track here. We can probably just change get_or_create() to update_or_create() to handle both conditions. I'll submit the change for review in a minute.

@jeremystretch commented on GitHub (Feb 16, 2024): > I believe this could be solved easily by altering the delete mechanism in SyncDataMixin to delete all records that should not be present in the database at the end of save() and delete(). I could check this locally and provide a PR if wanted … Thanks @alehaa. I think you're on the right track here. We can probably just change `get_or_create()` to `update_or_create()` to handle both conditions. I'll submit the change for review in a minute.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#9162