@@ -171,4 +171,127 @@ describe('With Policy: unique as id', () => {
171171 } )
172172 ) . toResolveTruthy ( ) ;
173173 } ) ;
174+
175+ it ( 'unique fields with to-many nested update' , async ( ) => {
176+ const { enhance } = await loadSchema (
177+ `
178+ model A {
179+ id Int @id @default(autoincrement())
180+ x Int
181+ y Int
182+ value Int
183+ bs B[]
184+ @@unique([x, y])
185+
186+ @@allow('read,create', true)
187+ @@allow('update,delete', value > 0)
188+ }
189+
190+ model B {
191+ id Int @id @default(autoincrement())
192+ value Int
193+ a A @relation(fields: [aId], references: [id])
194+ aId Int
195+
196+ @@allow('all', value > 0)
197+ }
198+ ` ,
199+ { logPrismaQuery : true }
200+ ) ;
201+
202+ const db = enhance ( ) ;
203+
204+ await db . a . create ( {
205+ data : { x : 1 , y : 1 , value : 1 , bs : { create : [ { id : 1 , value : 1 } ] } } ,
206+ } ) ;
207+
208+ await db . a . create ( {
209+ data : { x : 2 , y : 2 , value : 2 , bs : { create : [ { id : 2 , value : 2 } ] } } ,
210+ } ) ;
211+
212+ await db . a . update ( {
213+ where : { x_y : { x : 1 , y : 1 } } ,
214+ data : { bs : { updateMany : { data : { value : 3 } } } } ,
215+ } ) ;
216+
217+ // check b#1 is updated
218+ await expect ( db . b . findUnique ( { where : { id : 1 } } ) ) . resolves . toMatchObject ( { value : 3 } ) ;
219+
220+ // check b#2 is not affected
221+ await expect ( db . b . findUnique ( { where : { id : 2 } } ) ) . resolves . toMatchObject ( { value : 2 } ) ;
222+
223+ await db . a . update ( {
224+ where : { x_y : { x : 1 , y : 1 } } ,
225+ data : { bs : { deleteMany : { } } } ,
226+ } ) ;
227+
228+ // check b#1 is deleted
229+ await expect ( db . b . findUnique ( { where : { id : 1 } } ) ) . resolves . toBeNull ( ) ;
230+
231+ // check b#2 is not affected
232+ await expect ( db . b . findUnique ( { where : { id : 2 } } ) ) . resolves . toMatchObject ( { value : 2 } ) ;
233+ } ) ;
234+
235+ it ( 'unique fields with to-one nested update' , async ( ) => {
236+ const { enhance } = await loadSchema (
237+ `
238+ model A {
239+ id Int @id @default(autoincrement())
240+ x Int
241+ y Int
242+ value Int
243+ b B?
244+ @@unique([x, y])
245+
246+ @@allow('read,create', true)
247+ @@allow('update,delete', value > 0)
248+ }
249+
250+ model B {
251+ id Int @id @default(autoincrement())
252+ value Int
253+ a A @relation(fields: [aId], references: [id])
254+ aId Int @unique
255+
256+ @@allow('all', value > 0)
257+ }
258+ `
259+ ) ;
260+
261+ const db = enhance ( ) ;
262+
263+ await db . a . create ( {
264+ data : { x : 1 , y : 1 , value : 1 , b : { create : { id : 1 , value : 1 } } } ,
265+ } ) ;
266+
267+ await db . a . create ( {
268+ data : { x : 2 , y : 2 , value : 2 , b : { create : { id : 2 , value : 2 } } } ,
269+ } ) ;
270+
271+ await db . a . update ( {
272+ where : { x_y : { x : 1 , y : 1 } } ,
273+ data : { b : { update : { data : { value : 3 } } } } ,
274+ } ) ;
275+
276+ // check b#1 is updated
277+ await expect ( db . b . findUnique ( { where : { id : 1 } } ) ) . resolves . toMatchObject ( { value : 3 } ) ;
278+
279+ // check b#2 is not affected
280+ await expect ( db . b . findUnique ( { where : { id : 2 } } ) ) . resolves . toMatchObject ( { value : 2 } ) ;
281+
282+ await db . a . update ( {
283+ where : { x_y : { x : 1 , y : 1 } } ,
284+ data : { b : { delete : true } } ,
285+ } ) ;
286+
287+ // check b#1 is deleted
288+ await expect ( db . b . findUnique ( { where : { id : 1 } } ) ) . resolves . toBeNull ( ) ;
289+ await expect ( db . a . findUnique ( { where : { x_y : { x : 1 , y : 1 } } , include : { b : true } } ) ) . resolves . toMatchObject ( {
290+ b : null ,
291+ } ) ;
292+
293+ // check b#2 is not affected
294+ await expect ( db . b . findUnique ( { where : { id : 2 } } ) ) . resolves . toMatchObject ( { value : 2 } ) ;
295+ await expect ( db . a . findUnique ( { where : { x_y : { x : 2 , y : 2 } } , include : { b : true } } ) ) . resolves . toBeTruthy ( ) ;
296+ } ) ;
174297} ) ;
0 commit comments