nautobot-app-netbox-importer icon indicating copy to clipboard operation
nautobot-app-netbox-importer copied to clipboard

Unable to import due to foreign key constraint violation

Open helderbetiol opened this issue 1 year ago • 1 comments

Environment

  • Python version: 3.11.7
  • Nautobot version: 2.0.6
  • nautobot-netbox-importer version: 2.0.0 (develop branch, commit dee34e6 · 2 weeks ago)

Expected Behavior

To successfully import a netbox (v3.7.1) dump.

Observed Behavior

When importing, I get the following error also related to foreign key constraints and a cable_id not being present in the dcim_cable table:

2024-05-24 08:15:44 [info     ] Sync complete                  dst=<NautobotAdapter "Nautobot"> flags=<DiffSyncFlags.NONE: 0> src=<NetBoxAdapter "NetBox">
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 242, in _commit
    return self.connection.commit()
           ^^^^^^^^^^^^^^^^^^^^^^^^
psycopg2.errors.ForeignKeyViolation: insert or update on table "dcim_interface" violates foreign key constraint "dcim_interface_cable_id_1b264edb_fk_dcim_cable_id"
DETAIL:  Key (cable_id)=(fa7ade1f-810c-555b-96f8-8d500622da28) is not present in table "dcim_cable".


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/bin/nautobot-server", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/usr/local/lib/python3.11/site-packages/nautobot/core/cli/__init__.py", line 53, in main
    run_app(
  File "/usr/local/lib/python3.11/site-packages/nautobot/core/runner/runner.py", line 297, in run_app
    management.execute_from_command_line([runner_name, command] + command_args)
  File "/usr/local/lib/python3.11/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.11/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.11/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.11/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/source/nautobot_netbox_importer/management/commands/import_netbox.py", line 89, in handle
    adapter.import_to_nautobot()
  File "/source/nautobot_netbox_importer/diffsync/adapters/netbox.py", line 97, in import_to_nautobot
    self._atomic_import()
  File "/usr/local/lib/python3.11/contextlib.py", line 80, in inner
    with self._recreate_cm():
  File "/usr/local/lib/python3.11/site-packages/django/db/transaction.py", line 246, in __exit__
    connection.commit()
  File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 33, in inner
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 266, in commit
    self._commit()
  File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 241, in _commit
    with self.wrap_database_errors:
  File "/usr/local/lib/python3.11/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 242, in _commit
    return self.connection.commit()
           ^^^^^^^^^^^^^^^^^^^^^^^^
django.db.utils.IntegrityError: insert or update on table "dcim_interface" violates foreign key constraint "dcim_interface_cable_id_1b264edb_fk_dcim_cable_id"
DETAIL:  Key (cable_id)=(fa7ade1f-810c-555b-96f8-8d500622da28) is not present in table "dcim_cable".

When looking at the mappings.txt file, I find the following concerning this UUID:

fa7ade1f-810c-555b-96f8-8d500622da28 | SaveFailed | "{'_state': <django.db.models.base.ModelState object at 0xffff6db6f610>, 'id': UUID('fa7ade1f-810c-555b-96f8-8d500622da28'), 'created': datetime.datetime(2024, 5, 24, 8, 9, 6, 88363, tzinfo=<UTC>), 'last_updated': datetime.datetime(2024, 5, 24, 8, 9, 6, 88373, tzinfo=<UTC>), '_custom_field_data': {}, 'termination_a_type_id': None, 'termination_a_id': None, 'termination_b_type_id': 13, 'termination_b_id': UUID('8dfd2cf3-04d8-5d55-9cc1-7496b6f57d89'), 'type': 'cat6', 'status_id': UUID('318a7220-4327-48ca-876c-d1389a81758c'), 'label': '', 'color': '', 'length': None, 'length_unit': '', '_abs_length': None, '_termination_a_device_id': None, '_termination_b_device_id': UUID('51d4e2b2-cf93-5dc1-9848-a0e185b7f8b0'), '_pk': UUID('fa7ade1f-810c-555b-96f8-8d500622da28'), '_orig_status': None}" | "null value in column \"termination_a_id\" of relation \"dcim_cable\" violates not-null constraint\nDETAIL:  Failing row contains (fa7ade1f-810c-555b-96f8-8d500622da28, 2024-05-24 08:09:06.088363+00, 2024-05-24 08:09:06.088373+00, {}, null, 8dfd2cf3-04d8-5d55-9cc1-7496b6f57d89, cat6, , , null, , null, null, 51d4e2b2-cf93-5dc1-9848-a0e185b7f8b0, 318a7220-4327-48ca-876c-d1389a81758c, null, 13).\n"

helderbetiol avatar May 28 '24 13:05 helderbetiol

Looks like you have a cable that isn't connected to anything on the far end. null value in column \"termination_a_id\" of relation \"dcim_cable\" violates not-null constraint

glennmatthews avatar Jun 17 '24 19:06 glennmatthews

Closing since there's been no answer since June. Please feel free to re-open and provide sample data if possible for any further investigation.

cmsirbu avatar Sep 16 '24 19:09 cmsirbu