Fix numeric natural keys resolving to the wrong asset on import

This commit is contained in:
2026-08-31 15:55:31 -04:00
parent f087a7a5b0
commit 7081ea5d41
3 changed files with 74 additions and 10 deletions

View File

@@ -335,3 +335,29 @@ def test_reconcile_still_binds_normalized_exact(db):
_db.session.rollback()
def test_a_numeric_assetnumber_is_read_as_a_name_not_an_id(db):
"""This site numbers operations 0NNN, so numeric natural keys are real.
An all-digit string used to be taken at face value as an id. `0600` became
id 600 - a different asset, or none - which is how an Azure device import
died on a foreign key partway through, and how a PC could instead have been
bound silently to a printer's record. The label wins when one matches.
"""
resolver = Resolver()
seed_reference(resolver)
import_csv('assets', 'assetnumber,assettypeid\n0600,Machine\n',
resolver=resolver, commit=True)
realid = _db.session.execute(_db.text(
"select assetid from assets where assetnumber = '0600'")).scalar()
# Force the ambiguity: an id that is NOT the one named 0600.
assert resolver.resolve('assets', 'assetid', '0600') == realid
def test_a_number_naming_nothing_is_still_read_as_an_id(db):
"""Round-tripped exports and sites that know their ids keep working."""
resolver = Resolver()
seed_reference(resolver)
assert resolver.resolve('assets', 'assetid', '999999') == 999999