-
Notifications
You must be signed in to change notification settings - Fork 213
/
relations_negative.rs
1107 lines (961 loc) Β· 42.4 KB
/
relations_negative.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use crate::{common::*, with_header, Provider};
#[test]
fn fail_if_ambiguous_relation_fields_do_not_specify_a_name() {
let dml = indoc! {r#"
model Todo {
id Int @id
comments Comment[]
comments2 Comment[]
}
model Comment {
id Int @id
text String
}
"#};
let expect = expect![[r#"
[1;91merror[0m: [1mError validating model "Todo": Ambiguous relation detected. The fields `comments` and `comments2` in model `Todo` both refer to `Comment`. Please provide different relation names for them by adding `@relation(<name>).[0m
[1;94m-->[0m [4mschema.prisma:3[0m
[1;94m | [0m
[1;94m 2 | [0m id Int @id
[1;94m 3 | [0m [1;91mcomments Comment[][0m
[1;94m 4 | [0m comments2 Comment[]
[1;94m | [0m
"#]];
expect.assert_eq(&parse_unwrap_err(dml));
}
#[test]
fn fail_if_naming_relation_fields_the_same_as_the_explicit_names() {
let dml = indoc! {r#"
model Club {
id Int @id @default(autoincrement())
adminId Int @map("admin_id")
admin User @relation(fields: [adminId], references: [id])
members User[] @relation("ClubToUser")
@@map("clubs")
}
model User {
id Int @id @default(autoincrement())
clubs_clubsTousers Club[] @relation("ClubToUser")
ownedClubs Club[]
@@map("users")
}
"#};
let expect = expect![[r#"
[1;91merror[0m: [1mError validating model "Club": Ambiguous relation detected. The fields `admin` and `members` in model `Club` both refer to `User`. Please provide different relation names for them by adding `@relation(<name>).[0m
[1;94m-->[0m [4mschema.prisma:4[0m
[1;94m | [0m
[1;94m 3 | [0m adminId Int @map("admin_id")
[1;94m 4 | [0m [1;91madmin User @relation(fields: [adminId], references: [id])[0m
[1;94m 5 | [0m members User[] @relation("ClubToUser")
[1;94m | [0m
"#]];
expect.assert_eq(&parse_unwrap_err(dml));
}
#[test]
fn must_error_when_non_existing_fields_are_used() {
let dml = indoc! {r#"
model User {
id Int @id
firstName String
lastName String
posts Post[]
@@unique([firstName, lastName])
}
model Post {
id Int @id
text String
user User @relation(fields: [authorFirstName, authorLastName], references: [firstName, lastName])
}
"#};
let expect = expect![[r#"
[1;91merror[0m: [1mError validating: The argument fields must refer only to existing fields. The following fields do not exist in this model: authorFirstName, authorLastName[0m
[1;94m-->[0m [4mschema.prisma:13[0m
[1;94m | [0m
[1;94m12 | [0m text String
[1;94m13 | [0m user User @relation(fields: [1;91m[authorFirstName, authorLastName][0m, references: [firstName, lastName])
[1;94m | [0m
"#]];
expect.assert_eq(&parse_unwrap_err(dml));
}
#[test]
fn should_fail_on_ambiguous_relations_with_automatic_names_1() {
let dml = indoc! {r#"
model User {
id Int @id
posts Post[]
more_posts Post[]
}
model Post {
post_id Int @id
}
"#};
let expect = expect![[r#"
[1;91merror[0m: [1mError validating model "User": Ambiguous relation detected. The fields `posts` and `more_posts` in model `User` both refer to `Post`. Please provide different relation names for them by adding `@relation(<name>).[0m
[1;94m-->[0m [4mschema.prisma:3[0m
[1;94m | [0m
[1;94m 2 | [0m id Int @id
[1;94m 3 | [0m [1;91mposts Post[][0m
[1;94m 4 | [0m more_posts Post[]
[1;94m | [0m
"#]];
expect.assert_eq(&parse_unwrap_err(dml));
}
#[test]
fn should_fail_on_colliding_implicit_self_relations() {
let dml = indoc! {r#"
model User {
id Int @id @default(autoincrement())
name String?
husband User? @relation("MarriagePartners")
wife User @relation("MarriagePartners")
teacher User? @relation("TeacherStudents")
students User[] @relation("TeacherStudents")
}
"#};
let expect = expect![[r#"
[1;91merror[0m: [1mError parsing attribute "@relation": The relation fields `wife` on Model `User` and `husband` on Model `User` do not provide the `fields` argument in the @relation attribute. You have to provide it on one of the two fields.[0m
[1;94m-->[0m [4mschema.prisma:6[0m
[1;94m | [0m
[1;94m 5 | [0m husband User? @relation("MarriagePartners")
[1;94m 6 | [0m [1;91mwife User @relation("MarriagePartners")[0m
[1;94m 7 | [0m
[1;94m | [0m
[1;91merror[0m: [1mError parsing attribute "@relation": The relation fields `husband` on Model `User` and `wife` on Model `User` do not provide the `fields` argument in the @relation attribute. You have to provide it on one of the two fields.[0m
[1;94m-->[0m [4mschema.prisma:5[0m
[1;94m | [0m
[1;94m 4 | [0m
[1;94m 5 | [0m [1;91mhusband User? @relation("MarriagePartners")[0m
[1;94m 6 | [0m wife User @relation("MarriagePartners")
[1;94m | [0m
[1;91merror[0m: [1mError parsing attribute "@relation": The relation fields `wife` on Model `User` and `husband` on Model `User` do not provide the `references` argument in the @relation attribute. You have to provide it on one of the two fields.[0m
[1;94m-->[0m [4mschema.prisma:6[0m
[1;94m | [0m
[1;94m 5 | [0m husband User? @relation("MarriagePartners")
[1;94m 6 | [0m [1;91mwife User @relation("MarriagePartners")[0m
[1;94m 7 | [0m
[1;94m | [0m
[1;91merror[0m: [1mError parsing attribute "@relation": The relation fields `husband` on Model `User` and `wife` on Model `User` do not provide the `references` argument in the @relation attribute. You have to provide it on one of the two fields.[0m
[1;94m-->[0m [4mschema.prisma:5[0m
[1;94m | [0m
[1;94m 4 | [0m
[1;94m 5 | [0m [1;91mhusband User? @relation("MarriagePartners")[0m
[1;94m 6 | [0m wife User @relation("MarriagePartners")
[1;94m | [0m
[1;91merror[0m: [1mError parsing attribute "@relation": The relation field `teacher` on Model `User` must specify the `fields` argument in the @relation attribute. You can run `prisma format` to fix this automatically.[0m
[1;94m-->[0m [4mschema.prisma:8[0m
[1;94m | [0m
[1;94m 7 | [0m
[1;94m 8 | [0m [1;91mteacher User? @relation("TeacherStudents")[0m
[1;94m 9 | [0m students User[] @relation("TeacherStudents")
[1;94m | [0m
[1;91merror[0m: [1mError parsing attribute "@relation": The relation field `teacher` on Model `User` must specify the `references` argument in the @relation attribute.[0m
[1;94m-->[0m [4mschema.prisma:8[0m
[1;94m | [0m
[1;94m 7 | [0m
[1;94m 8 | [0m [1;91mteacher User? @relation("TeacherStudents")[0m
[1;94m 9 | [0m students User[] @relation("TeacherStudents")
[1;94m | [0m
"#]];
expect.assert_eq(&parse_unwrap_err(dml));
}
#[test]
fn should_fail_on_ambiguous_relations_with_automatic_names_2() {
// test case based on: https://github.com/prisma/prisma2/issues/976
let dml = indoc! {r#"
model User {
id Int @id
posts Post[]
}
model Post {
post_id Int @id
author1 User
author2 User
}
"#};
let expect = expect![[r#"
[1;91merror[0m: [1mError validating model "Post": Ambiguous relation detected. The fields `author1` and `author2` in model `Post` both refer to `User`. Please provide different relation names for them by adding `@relation(<name>).[0m
[1;94m-->[0m [4mschema.prisma:8[0m
[1;94m | [0m
[1;94m 7 | [0m post_id Int @id
[1;94m 8 | [0m [1;91mauthor1 User[0m
[1;94m 9 | [0m author2 User
[1;94m | [0m
"#]];
expect.assert_eq(&parse_unwrap_err(dml));
}
#[test]
fn should_fail_on_ambiguous_relations_with_manual_names_1() {
let dml = indoc! {r#"
model User {
id Int @id
posts Post[] @relation(name: "test")
more_posts Post[] @relation(name: "test")
}
model Post {
post_id Int @id
}
"#};
let expect = expect![[r#"
[1;91merror[0m: [1mError validating model "User": Wrongly named relation detected. The fields `posts` and `more_posts` in model `User` both use the same relation name. Please provide different relation names for them through `@relation(<name>).[0m
[1;94m-->[0m [4mschema.prisma:3[0m
[1;94m | [0m
[1;94m 2 | [0m id Int @id
[1;94m 3 | [0m [1;91mposts Post[] @relation(name: "test")[0m
[1;94m 4 | [0m more_posts Post[] @relation(name: "test")
[1;94m | [0m
"#]];
expect.assert_eq(&parse_unwrap_err(dml));
}
#[test]
fn should_fail_on_ambiguous_relations_with_manual_names_2() {
let dml = indoc! {r#"
model User {
id Int @id
posts Post[] @relation(name: "a")
more_posts Post[] @relation(name: "b")
some_posts Post[]
even_more_posts Post[] @relation(name: "a")
}
model Post {
post_id Int @id
}
"#};
let expect = expect![[r#"
[1;91merror[0m: [1mError validating model "User": Wrongly named relation detected. The fields `posts` and `even_more_posts` in model `User` both use the same relation name. Please provide different relation names for them through `@relation(<name>).[0m
[1;94m-->[0m [4mschema.prisma:3[0m
[1;94m | [0m
[1;94m 2 | [0m id Int @id
[1;94m 3 | [0m [1;91mposts Post[] @relation(name: "a")[0m
[1;94m 4 | [0m more_posts Post[] @relation(name: "b")
[1;94m | [0m
"#]];
expect.assert_eq(&parse_unwrap_err(dml));
}
#[test]
fn should_fail_on_ambiguous_self_relation() {
let dml = indoc! {r#"
model User {
id Int @id
father User
son User
mother User
}
"#};
let expect = expect![[r#"
[1;91merror[0m: [1mError validating model "User": Unnamed self relation detected. The fields `father`, `son` and `mother` in model `User` have no relation name. Please provide a relation name for one of them by adding `@relation(<name>).[0m
[1;94m-->[0m [4mschema.prisma:3[0m
[1;94m | [0m
[1;94m 2 | [0m id Int @id
[1;94m 3 | [0m [1;91mfather User[0m
[1;94m 4 | [0m son User
[1;94m | [0m
"#]];
expect.assert_eq(&parse_unwrap_err(dml));
}
#[test]
fn should_fail_on_ambiguous_self_relation_with_two_fields() {
let dml = indoc! {r#"
model User {
id Int @id
child User
mother User
}
"#};
let expect = expect![[r#"
[1;91merror[0m: [1mError validating model "User": Ambiguous self relation detected. The fields `child` and `mother` in model `User` both refer to `User`. If they are part of the same relation add the same relation name for them with `@relation(<name>)`.[0m
[1;94m-->[0m [4mschema.prisma:3[0m
[1;94m | [0m
[1;94m 2 | [0m id Int @id
[1;94m 3 | [0m [1;91mchild User[0m
[1;94m 4 | [0m mother User
[1;94m | [0m
"#]];
expect.assert_eq(&parse_unwrap_err(dml));
}
#[test]
fn should_fail_on_ambiguous_named_self_relation() {
let dml = indoc! {r#"
model User {
id Int @id
father User @relation(name: "family")
son User @relation(name: "family")
mother User @relation(name: "family")
}
"#};
let expect = expect![[r#"
[1;91merror[0m: [1mError validating model "User": Wrongly named self relation detected. The fields `father`, `son` and `mother` in model `User` have the same relation name. At most two relation fields can belong to the same relation and therefore have the same name. Please assign a different relation name to one of them.[0m
[1;94m-->[0m [4mschema.prisma:3[0m
[1;94m | [0m
[1;94m 2 | [0m id Int @id
[1;94m 3 | [0m [1;91mfather User @relation(name: "family")[0m
[1;94m 4 | [0m son User @relation(name: "family")
[1;94m | [0m
"#]];
expect.assert_eq(&parse_unwrap_err(dml));
}
#[test]
fn should_fail_on_conflicting_back_relation_field_name() {
let dml = indoc! {r#"
model User {
id Int @id
posts Post[] @relation(name: "test")
more_posts Post[]
}
model Post {
post_id Int @id
User User @relation(name: "test")
}
"#};
let expect = expect![[r#"
[1;91merror[0m: [1mError validating field `more_posts` in model `User`: The relation field `more_posts` on Model `User` is missing an opposite relation field on the model `Post`. Either run `prisma format` or add it manually.[0m
[1;94m-->[0m [4mschema.prisma:4[0m
[1;94m | [0m
[1;94m 3 | [0m posts Post[] @relation(name: "test")
[1;94m 4 | [0m [1;91mmore_posts Post[][0m
[1;94m 5 | [0m}
[1;94m | [0m
[1;91merror[0m: [1mError parsing attribute "@relation": The relation field `User` on Model `Post` must specify the `fields` argument in the @relation attribute. You can run `prisma format` to fix this automatically.[0m
[1;94m-->[0m [4mschema.prisma:9[0m
[1;94m | [0m
[1;94m 8 | [0m post_id Int @id
[1;94m 9 | [0m [1;91mUser User @relation(name: "test")[0m
[1;94m10 | [0m}
[1;94m | [0m
[1;91merror[0m: [1mError parsing attribute "@relation": The relation field `User` on Model `Post` must specify the `references` argument in the @relation attribute.[0m
[1;94m-->[0m [4mschema.prisma:9[0m
[1;94m | [0m
[1;94m 8 | [0m post_id Int @id
[1;94m 9 | [0m [1;91mUser User @relation(name: "test")[0m
[1;94m10 | [0m}
[1;94m | [0m
"#]];
expect.assert_eq(&parse_unwrap_err(dml));
}
#[test]
//todo formatter should make an offer
fn should_fail_when_relation_attribute_is_missing_for_one_to_one_relations() {
// Post is lower that User. So the references should be stored in Post.
let dml = indoc! {r#"
model User {
user_id Int @id
post Post
}
model Post {
post_id Int @id
user User
}
"#};
let expect = expect![[r#"
[1;91merror[0m: [1mError parsing attribute "@relation": The relation fields `user` on Model `Post` and `post` on Model `User` do not provide the `fields` argument in the @relation attribute. You have to provide it on one of the two fields.[0m
[1;94m-->[0m [4mschema.prisma:8[0m
[1;94m | [0m
[1;94m 7 | [0m post_id Int @id
[1;94m 8 | [0m [1;91muser User[0m
[1;94m 9 | [0m}
[1;94m | [0m
[1;91merror[0m: [1mError parsing attribute "@relation": The relation fields `post` on Model `User` and `user` on Model `Post` do not provide the `fields` argument in the @relation attribute. You have to provide it on one of the two fields.[0m
[1;94m-->[0m [4mschema.prisma:3[0m
[1;94m | [0m
[1;94m 2 | [0m user_id Int @id
[1;94m 3 | [0m [1;91mpost Post[0m
[1;94m 4 | [0m}
[1;94m | [0m
[1;91merror[0m: [1mError parsing attribute "@relation": The relation fields `user` on Model `Post` and `post` on Model `User` do not provide the `references` argument in the @relation attribute. You have to provide it on one of the two fields.[0m
[1;94m-->[0m [4mschema.prisma:8[0m
[1;94m | [0m
[1;94m 7 | [0m post_id Int @id
[1;94m 8 | [0m [1;91muser User[0m
[1;94m 9 | [0m}
[1;94m | [0m
[1;91merror[0m: [1mError parsing attribute "@relation": The relation fields `post` on Model `User` and `user` on Model `Post` do not provide the `references` argument in the @relation attribute. You have to provide it on one of the two fields.[0m
[1;94m-->[0m [4mschema.prisma:3[0m
[1;94m | [0m
[1;94m 2 | [0m user_id Int @id
[1;94m 3 | [0m [1;91mpost Post[0m
[1;94m 4 | [0m}
[1;94m | [0m
"#]];
expect.assert_eq(&parse_unwrap_err(dml));
}
#[test]
fn should_fail_on_conflicting_generated_back_relation_fields() {
// More specifically, this should not panic.
let dml = indoc! {r#"
model Todo {
id Int @id
author Owner @relation(name: "AuthorTodo")
delegatedTo Owner? @relation(name: "DelegatedToTodo")
}
model Owner {
id Int @id
todos Todo[]
}
"#};
let expect = expect![[r#"
[1;91merror[0m: [1mError validating field `author` in model `Todo`: The relation field `author` on Model `Todo` is missing an opposite relation field on the model `Owner`. Either run `prisma format` or add it manually.[0m
[1;94m-->[0m [4mschema.prisma:3[0m
[1;94m | [0m
[1;94m 2 | [0m id Int @id
[1;94m 3 | [0m [1;91mauthor Owner @relation(name: "AuthorTodo")[0m
[1;94m 4 | [0m delegatedTo Owner? @relation(name: "DelegatedToTodo")
[1;94m | [0m
[1;91merror[0m: [1mError validating field `delegatedTo` in model `Todo`: The relation field `delegatedTo` on Model `Todo` is missing an opposite relation field on the model `Owner`. Either run `prisma format` or add it manually.[0m
[1;94m-->[0m [4mschema.prisma:4[0m
[1;94m | [0m
[1;94m 3 | [0m author Owner @relation(name: "AuthorTodo")
[1;94m 4 | [0m [1;91mdelegatedTo Owner? @relation(name: "DelegatedToTodo")[0m
[1;94m 5 | [0m}
[1;94m | [0m
[1;91merror[0m: [1mError validating field `todos` in model `Owner`: The relation field `todos` on Model `Owner` is missing an opposite relation field on the model `Todo`. Either run `prisma format` or add it manually.[0m
[1;94m-->[0m [4mschema.prisma:9[0m
[1;94m | [0m
[1;94m 8 | [0m id Int @id
[1;94m 9 | [0m [1;91mtodos Todo[][0m
[1;94m10 | [0m}
[1;94m | [0m
"#]];
expect.assert_eq(&parse_unwrap_err(dml));
}
//reformat implicit relations test files
//todo this talked about adding backrelation fields but was adding forward field + scalarfield
#[test]
fn must_generate_forward_relation_fields_for_named_relation_fields() {
//reject, hint to prisma format, add scalar field and relation field, validate again
let dml = indoc! {r#"
model Todo {
id Int @id
assignees User[] @relation(name: "AssignedTodos")
}
model User {
id Int @id
}
"#};
let expect = expect![[r#"
[1;91merror[0m: [1mError validating field `assignees` in model `Todo`: The relation field `assignees` on Model `Todo` is missing an opposite relation field on the model `User`. Either run `prisma format` or add it manually.[0m
[1;94m-->[0m [4mschema.prisma:3[0m
[1;94m | [0m
[1;94m 2 | [0m id Int @id
[1;94m 3 | [0m [1;91massignees User[] @relation(name: "AssignedTodos")[0m
[1;94m 4 | [0m}
[1;94m | [0m
"#]];
expect.assert_eq(&parse_unwrap_err(dml));
}
// todo this is also accepted and adds a postId scalar field under the hood on PostableEntity
// is almost the exact same case as the one above (minus the relationname), but reported as a bug and also understood by harshit as such
#[test]
fn issue4850() {
//reject, hint to prisma format, add scalar field and relation field, validate again
let dml = indoc! {r#"
model PostableEntity {
id String @id
}
model Post {
id String @id
postableEntities PostableEntity[]
}
"#};
let expect = expect![[r#"
[1;91merror[0m: [1mError validating field `postableEntities` in model `Post`: The relation field `postableEntities` on Model `Post` is missing an opposite relation field on the model `PostableEntity`. Either run `prisma format` or add it manually.[0m
[1;94m-->[0m [4mschema.prisma:7[0m
[1;94m | [0m
[1;94m 6 | [0m id String @id
[1;94m 7 | [0m [1;91mpostableEntities PostableEntity[][0m
[1;94m 8 | [0m}
[1;94m | [0m
"#]];
expect.assert_eq(&parse_unwrap_err(dml));
}
//todo I think this should be fine and just add the @relation and relationname to the backrelation field
// but this interprets the dm as containing two relations.
#[test]
fn issue4822() {
//reject, ask to name custom_Post relation
let dml = indoc! {r#"
model Post {
id Int @id
user_id Int @unique
custom_User User @relation("CustomName", fields: [user_id], references: [id])
}
model User {
id Int @id
custom_Post Post?
}
"#};
let expect = expect![[r#"
[1;91merror[0m: [1mError validating field `custom_User` in model `Post`: The relation field `custom_User` on Model `Post` is missing an opposite relation field on the model `User`. Either run `prisma format` or add it manually.[0m
[1;94m-->[0m [4mschema.prisma:4[0m
[1;94m | [0m
[1;94m 3 | [0m user_id Int @unique
[1;94m 4 | [0m [1;91mcustom_User User @relation("CustomName", fields: [user_id], references: [id])[0m
[1;94m 5 | [0m}
[1;94m | [0m
[1;91merror[0m: [1mError validating field `custom_Post` in model `User`: The relation field `custom_Post` on Model `User` is missing an opposite relation field on the model `Post`. Either run `prisma format` or add it manually.[0m
[1;94m-->[0m [4mschema.prisma:9[0m
[1;94m | [0m
[1;94m 8 | [0m id Int @id
[1;94m 9 | [0m [1;91mcustom_Post Post?[0m
[1;94m10 | [0m}
[1;94m | [0m
"#]];
expect_error(dml, &expect);
}
#[test]
fn issue5216() {
let dml = indoc! {r#"
model user {
id String @id
email String @unique
organization organization? @relation(references: [id])
}
model organization {
id String @id
users user[]
}
"#};
let expect = expect![[r#"
[1;91merror[0m: [1mError parsing attribute "@relation": The relation field `organization` on Model `user` must specify the `fields` argument in the @relation attribute. You can run `prisma format` to fix this automatically.[0m
[1;94m-->[0m [4mschema.prisma:4[0m
[1;94m | [0m
[1;94m 3 | [0m email String @unique
[1;94m 4 | [0m [1;91morganization organization? @relation(references: [id])[0m
[1;94m 5 | [0m}
[1;94m | [0m
"#]];
expect.assert_eq(&parse_unwrap_err(dml));
}
//todo this is also accepted but will under the hood point the createdBy relationfield to the same userId scalar
// as the user relationfield
// duplicate of 5540
// comment by matt:
// We don't want to remove the formatting feature that adds @relation and foreign key, this is a beloved feature.
// We want the validator to ensure that @relation always exists and links to a valid field.
// If the formatter is unable to correctly add @relation because of an ambiguity (e.g. user & createdBy), it shouldn't try. The validator will just tell you that you're missing @relation and need to add them in by hand to resolve the issue.
#[test]
fn issue5069() {
// reject
let dml = indoc! {r#"
model Code {
id String @id
createdById String?
createdBy User?
userId String?
user User? @relation("code", fields: [userId], references: [id])
}
model User {
id String @id
codes Code[] @relation("code")
}
"#};
let expect = expect![[r#"
[1;91merror[0m: [1mError validating field `createdBy` in model `Code`: The relation field `createdBy` on Model `Code` is missing an opposite relation field on the model `User`. Either run `prisma format` or add it manually.[0m
[1;94m-->[0m [4mschema.prisma:4[0m
[1;94m | [0m
[1;94m 3 | [0m createdById String?
[1;94m 4 | [0m [1;91mcreatedBy User?[0m
[1;94m 5 | [0m
[1;94m | [0m
"#]];
expect.assert_eq(&parse_unwrap_err(dml));
}
#[test]
fn must_add_referenced_fields_on_both_sides_for_one_to_many_relations() {
let dml = indoc! {r#"
model User {
user_id Int @id
posts Post[]
}
model Post {
post_id Int @id
user User
}
"#};
let expect = expect![[r#"
[1;91merror[0m: [1mError parsing attribute "@relation": The relation field `user` on Model `Post` must specify the `fields` argument in the @relation attribute. You can run `prisma format` to fix this automatically.[0m
[1;94m-->[0m [4mschema.prisma:8[0m
[1;94m | [0m
[1;94m 7 | [0m post_id Int @id
[1;94m 8 | [0m [1;91muser User[0m
[1;94m 9 | [0m}
[1;94m | [0m
[1;91merror[0m: [1mError parsing attribute "@relation": The relation field `user` on Model `Post` must specify the `references` argument in the @relation attribute.[0m
[1;94m-->[0m [4mschema.prisma:8[0m
[1;94m | [0m
[1;94m 7 | [0m post_id Int @id
[1;94m 8 | [0m [1;91muser User[0m
[1;94m 9 | [0m}
[1;94m | [0m
"#]];
expect.assert_eq(&parse_unwrap_err(dml));
// prove that lexicographic order does not have an influence.
let dml = indoc! {r#"
model User {
user_id Int @id
post Post
}
model Post {
post_id Int @id
users User[]
}
"#};
let expect = expect![[r#"
[1;91merror[0m: [1mError parsing attribute "@relation": The relation field `post` on Model `User` must specify the `fields` argument in the @relation attribute. You can run `prisma format` to fix this automatically.[0m
[1;94m-->[0m [4mschema.prisma:3[0m
[1;94m | [0m
[1;94m 2 | [0m user_id Int @id
[1;94m 3 | [0m [1;91mpost Post[0m
[1;94m 4 | [0m}
[1;94m | [0m
[1;91merror[0m: [1mError parsing attribute "@relation": The relation field `post` on Model `User` must specify the `references` argument in the @relation attribute.[0m
[1;94m-->[0m [4mschema.prisma:3[0m
[1;94m | [0m
[1;94m 2 | [0m user_id Int @id
[1;94m 3 | [0m [1;91mpost Post[0m
[1;94m 4 | [0m}
[1;94m | [0m
"#]];
expect.assert_eq(&parse_unwrap_err(dml));
}
#[test]
fn should_fail_on_missing_embed_ids_on_self_relations() {
let dml = indoc! {r#"
model Human {
id Int @id
father Human? @relation("paternity")
son Human? @relation("paternity")
}
"#};
let expect = expect![[r#"
[1;91merror[0m: [1mError parsing attribute "@relation": The relation fields `father` on Model `Human` and `son` on Model `Human` do not provide the `fields` argument in the @relation attribute. You have to provide it on one of the two fields.[0m
[1;94m-->[0m [4mschema.prisma:3[0m
[1;94m | [0m
[1;94m 2 | [0m id Int @id
[1;94m 3 | [0m [1;91mfather Human? @relation("paternity")[0m
[1;94m 4 | [0m son Human? @relation("paternity")
[1;94m | [0m
[1;91merror[0m: [1mError parsing attribute "@relation": The relation fields `son` on Model `Human` and `father` on Model `Human` do not provide the `fields` argument in the @relation attribute. You have to provide it on one of the two fields.[0m
[1;94m-->[0m [4mschema.prisma:4[0m
[1;94m | [0m
[1;94m 3 | [0m father Human? @relation("paternity")
[1;94m 4 | [0m [1;91mson Human? @relation("paternity")[0m
[1;94m 5 | [0m}
[1;94m | [0m
[1;91merror[0m: [1mError parsing attribute "@relation": The relation fields `father` on Model `Human` and `son` on Model `Human` do not provide the `references` argument in the @relation attribute. You have to provide it on one of the two fields.[0m
[1;94m-->[0m [4mschema.prisma:3[0m
[1;94m | [0m
[1;94m 2 | [0m id Int @id
[1;94m 3 | [0m [1;91mfather Human? @relation("paternity")[0m
[1;94m 4 | [0m son Human? @relation("paternity")
[1;94m | [0m
[1;91merror[0m: [1mError parsing attribute "@relation": The relation fields `son` on Model `Human` and `father` on Model `Human` do not provide the `references` argument in the @relation attribute. You have to provide it on one of the two fields.[0m
[1;94m-->[0m [4mschema.prisma:4[0m
[1;94m | [0m
[1;94m 3 | [0m father Human? @relation("paternity")
[1;94m 4 | [0m [1;91mson Human? @relation("paternity")[0m
[1;94m 5 | [0m}
[1;94m | [0m
"#]];
expect.assert_eq(&parse_unwrap_err(dml));
}
#[test]
fn mapping_foreign_keys_with_a_name_that_is_too_long_should_error() {
let dml = with_header(
indoc! {r#"
model User {
id Int @id
posts Post[]
}
model Post {
post_id Int @id
user_id Int
user User @relation(fields:[post_id], references: [id], map: "IfYouAreGoingToPickTheNameYourselfYouShouldReallyPickSomethingShortAndSweetInsteadOfASuperLongNameViolatingLengthLimits")
}
"#},
Provider::Postgres,
&[],
);
let expect = expect![[r#"
[1;91merror[0m: [1mError validating model "Post": The constraint name 'IfYouAreGoingToPickTheNameYourselfYouShouldReallyPickSomethingShortAndSweetInsteadOfASuperLongNameViolatingLengthLimits' specified in the `map` argument for the `@relation` constraint is too long for your chosen provider. The maximum allowed length is 63 bytes.[0m
[1;94m-->[0m [4mschema.prisma:19[0m
[1;94m | [0m
[1;94m18 | [0m user_id Int
[1;94m19 | [0m user User [1;91m@relation(fields:[post_id], references: [id], map: "IfYouAreGoingToPickTheNameYourselfYouShouldReallyPickSomethingShortAndSweetInsteadOfASuperLongNameViolatingLengthLimits")[0m
[1;94m | [0m
"#]];
expect.assert_eq(&parse_unwrap_err(&dml));
}
#[test]
fn mapping_foreign_keys_on_sqlite_should_error() {
let dml = indoc! {r#"
datasource test {
provider = "sqlite"
url = "file:."
}
model User {
id Int @id
posts Post[]
}
model Post {
post_id Int @id
user_id Int
user User @relation(fields:[post_id], references: [id], map: "NoNamedForeignKeysOnSQLite")
}
"#};
let expect = expect![[r#"
[1;91merror[0m: [1mError parsing attribute "@relation": Your provider does not support named foreign keys.[0m
[1;94m-->[0m [4mschema.prisma:14[0m
[1;94m | [0m
[1;94m13 | [0m user_id Int
[1;94m14 | [0m user User [1;91m@relation(fields:[post_id], references: [id], map: "NoNamedForeignKeysOnSQLite")[0m
[1;94m | [0m
"#]];
expect.assert_eq(&parse_unwrap_err(dml));
}
#[test]
fn relation_field_in_composite_type_errors() {
let schema = r#"
datasource db {
provider = "mongodb"
url = "mongodb://"
}
type Address {
street String
test Test
}
model Test {
id Int @id
}
"#;
let expect = expect![[r#"
[1;91merror[0m: [1mError validating composite type "Address": Test refers to a model, making this a relation field. Relation fields inside composite types are not supported.[0m
[1;94m-->[0m [4mschema.prisma:9[0m
[1;94m | [0m
[1;94m 8 | [0m street String
[1;94m 9 | [0m test [1;91mTest[0m
[1;94m | [0m
"#]];
expect.assert_eq(&parse_unwrap_err(schema));
}
#[test]
fn relation_attribute_on_a_composite_field_errors() {
let schema = r#"
datasource db {
provider = "mongodb"
url = "mongodb://"
}
type Address {
street String
}
model Test {
id Int @id
addres Address? @relation("TestAddress")
}
"#;
let expect = expect![[r#"
[1;91merror[0m: [1mError parsing attribute "@relation": Invalid field type, not a relation.[0m
[1;94m-->[0m [4mschema.prisma:13[0m
[1;94m | [0m
[1;94m12 | [0m id Int @id
[1;94m13 | [0m addres Address? [1;91m@relation("TestAddress")[0m
[1;94m | [0m
[1;91merror[0m: [1mNo such argument.[0m
[1;94m-->[0m [4mschema.prisma:13[0m
[1;94m | [0m
[1;94m12 | [0m id Int @id
[1;94m13 | [0m addres Address? @relation([1;91m"TestAddress"[0m)
[1;94m | [0m
"#]];
expect.assert_eq(&parse_unwrap_err(schema));
}
#[test]
fn a_typoed_relation_should_fail_gracefully() {
let dml = indoc! {r#"
datasource db {
provider = "sqlserver"
url = env("DATABASE_URL")
}
model Test {
id Int @id
fk Int
testparent TestParent @relation(fields: [fk], references: [id])
}
model TestParent {
id Int @id
tests Test[]
fk Int
self TestParent @relation(fields: [fk], references: [id])
}
"#};
let expect = expect![[r#"
[1;91merror[0m: [1mError validating field `self` in model `TestParent`: The relation field `self` on Model `TestParent` is missing an opposite relation field on the model `TestParent`. Either run `prisma format` or add it manually.[0m
[1;94m-->[0m [4mschema.prisma:17[0m
[1;94m | [0m
[1;94m16 | [0m fk Int
[1;94m17 | [0m [1;91mself TestParent @relation(fields: [fk], references: [id])[0m
[1;94m18 | [0m}
[1;94m | [0m
"#]];
expect.assert_eq(&parse_unwrap_err(dml));
}
#[test]
fn cannot_use_references_with_many_to_many_relations() {
let dml = indoc! {r#"
model User {
id Int @id
post_id Int
posts Post[] @relation("a", references: [id])
}
model Post {
id Int @id
user_id Int
users User[] @relation("a", references: [id])
}
"#};
let expect = expect![[r#"
[1;91merror[0m: [1mError parsing attribute "@relation": Implicit many-to-many relation should not have references argument defined. Either remove it, or change the relation to one-to-many.[0m
[1;94m-->[0m [4mschema.prisma:10[0m
[1;94m | [0m
[1;94m 9 | [0m user_id Int
[1;94m10 | [0m [1;91musers User[] @relation("a", references: [id])[0m
[1;94m11 | [0m}
[1;94m | [0m
[1;91merror[0m: [1mError parsing attribute "@relation": Implicit many-to-many relation should not have references argument defined. Either remove it, or change the relation to one-to-many.[0m
[1;94m-->[0m [4mschema.prisma:4[0m
[1;94m | [0m
[1;94m 3 | [0m post_id Int
[1;94m 4 | [0m [1;91mposts Post[] @relation("a", references: [id])[0m
[1;94m 5 | [0m}
[1;94m | [0m
"#]];
expect.assert_eq(&parse_unwrap_err(dml));
}
#[test]
fn should_fail_if_not_using_unique_constraint_with_singular_one_to_one() {
let dml = indoc! {r#"
model A {
id Int @id
fk Int?
b B? @relation(fields: [fk], references: [id])
}
model B {
id Int @id
a A?
}
"#};
let expect = expect![[r#"
[1;91merror[0m: [1mError parsing attribute "@relation": A one-to-one relation must use unique fields on the defining side. Either add an `@unique` attribute to the field `fk`, or change the relation to one-to-many.[0m
[1;94m-->[0m [4mschema.prisma:4[0m
[1;94m | [0m
[1;94m 3 | [0m fk Int?
[1;94m 4 | [0m [1;91mb B? @relation(fields: [fk], references: [id])[0m
[1;94m 5 | [0m}
[1;94m | [0m
"#]];
expect.assert_eq(&parse_unwrap_err(dml));
}
#[test]
fn should_fail_if_not_using_unique_constraint_with_compound_one_to_one() {
let dml = indoc! {r#"
model A {
id Int @id
fk1 Int?
fk2 Int?
b B? @relation(fields: [fk1, fk2], references: [id1, id2])
}
model B {
id1 Int
id2 Int
a A?
@@id([id1, id2])
}
"#};
let expect = expect![[r#"