@@ -936,6 +936,44 @@ describe('seed replay — a column declared applyOn: create is written on first
936936 expect ( first . result . success ) . toBe ( false ) ;
937937 } ) ;
938938
939+ it ( 'a create-only reference declared BEFORE a reference column of a composite externalId is judged after the key resolves' , async ( ) => {
940+ // `billed_to` precedes `account` in the field map, so the reference loop
941+ // meets it first. Judged in that order, the row's key would still hold the
942+ // natural key 'Acme', match nothing, and the unresolvable `billed_to`
943+ // would be deferred to pass 2 — whose back-fill writes it onto the
944+ // matched row after all.
945+ const PERIOD = {
946+ name : 'apply_on_period' ,
947+ fields : {
948+ billed_to : { type : 'lookup' , reference : 'apply_on_account' } ,
949+ account : { type : 'lookup' , reference : 'apply_on_account' } ,
950+ period : { type : 'text' } ,
951+ amount : { type : 'number' } ,
952+ } ,
953+ } ;
954+ const { engine, store } = createFaithfulEngine ( ) ;
955+ const seedOf = ( records : Array < Record < string , unknown > > ) => [
956+ { object : ACCOUNT . name , externalId : 'name' , mode : 'upsert' , env : [ 'prod' , 'dev' , 'test' ] , records : [ { name : 'Acme' } , { name : 'Globex' } ] } ,
957+ {
958+ object : PERIOD . name , externalId : [ 'account' , 'period' ] , mode : 'upsert' , env : [ 'prod' , 'dev' , 'test' ] ,
959+ columns : { billed_to : { applyOn : 'create' } } , records,
960+ } ,
961+ ] as any ;
962+ const loadPeriods = ( records : Array < Record < string , unknown > > ) =>
963+ new SeedLoaderService ( engine , metadataOf ( ACCOUNT , PERIOD ) , createLogger ( ) ) . load ( { seeds : seedOf ( records ) , config : CONFIG } ) ;
964+ await loadPeriods ( [ { account : 'Acme' , period : '2026-10' , billed_to : 'Acme' , amount : 1 } ] ) ;
965+ ( engine . update as any ) . mockClear ( ) ;
966+
967+ const replay = await loadPeriods ( [ { account : 'Acme' , period : '2026-10' , billed_to : 'No Such Co' , amount : 2 } ] ) ;
968+
969+ expect ( replay . success ) . toBe ( true ) ;
970+ const periods = replay . results . find ( ( r ) => r . object === PERIOD . name ) ! ;
971+ expect ( periods . referencesDeferred ) . toBe ( 0 ) ;
972+ expect ( store [ PERIOD . name ] ) . toHaveLength ( 1 ) ;
973+ expect ( store [ PERIOD . name ] [ 0 ] ) . toMatchObject ( { amount : 2 , billed_to : accountId ( store , 'Acme' ) } ) ;
974+ expect ( updatesOf ( engine , PERIOD ) . some ( ( data : Record < string , unknown > ) => 'billed_to' in data ) ) . toBe ( false ) ;
975+ } ) ;
976+
939977 it ( 'a column of the externalId declared create-only is still matched on, and stays in the update' , async ( ) => {
940978 const { engine, store } = createFaithfulEngine ( ) ;
941979 const columns = { ...CREATE_ONLY , name : { applyOn : 'create' } } ;
0 commit comments