@@ -344,3 +344,43 @@ test('non-object null unions should not render as JSON', async () => {
344344 { name : 'Thomas' , nickName : 'Tom' , birthdate : new Date ( '1960-02-10T00:00:00Z' ) } ,
345345 ] ) ;
346346} ) ;
347+
348+ test ( 'updates & deletes should ignore previous @_pk session variable if no rows were affected' , async ( ) => {
349+ @entity . name ( 'model7' )
350+ class Model {
351+ id : number & PrimaryKey & AutoIncrement = 0 ;
352+
353+ constructor ( public name : string ) { }
354+ }
355+
356+ const database = await databaseFactory ( [ Model ] ) ;
357+ const model = new Model ( 'Peter' ) ;
358+ await database . persist ( model ) ;
359+ expect ( model . id ) . toEqual ( expect . any ( Number ) ) ;
360+
361+ // perform update & delete in single database connection to ensure @_pk session variable is shared
362+ // between multiple queries. outside a transaction, this is luck of the draw based on connection pooling.
363+ await database . transaction ( async txn => {
364+ {
365+ const result = await txn . query ( Model ) . filter ( { id : model . id } ) . patchMany ( { name : 'Paul' } ) ;
366+ expect ( result . primaryKeys [ 0 ] ) . toEqual ( { id : model . id } ) ;
367+ }
368+
369+ {
370+ const result = await txn . query ( Model ) . filter ( { id : model . id } ) . deleteMany ( ) ;
371+ expect ( result . primaryKeys [ 0 ] ) . toEqual ( { id : model . id } ) ;
372+ }
373+
374+ // there should be no ID returned if we try to update a non-existent record
375+ {
376+ const result = await txn . query ( Model ) . filter ( { id : model . id } ) . patchMany ( { name : 'Simon' } ) ;
377+ expect ( result . primaryKeys [ 0 ] ) . toBeUndefined ( ) ;
378+ }
379+
380+ // there should be no ID returned if we try to delete a non-existent record
381+ {
382+ const result = await txn . query ( Model ) . filter ( { id : model . id } ) . deleteMany ( ) ;
383+ expect ( result . primaryKeys [ 0 ] ) . toBeUndefined ( ) ;
384+ }
385+ } ) ;
386+ } ) ;
0 commit comments