Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL Server: The incoming request has too many parameters. The server supports a maximum of 2100 parameters. Reduce the number of parameters and resend the request. #18203

Open
JaapWeijland opened this issue Mar 3, 2023 · 25 comments
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: chunking topic: sql server Microsoft SQL Server

Comments

@JaapWeijland
Copy link

JaapWeijland commented Mar 3, 2023

Bug description

When requesting a model including the relations to another model, and when more than 2100 relations exist, SQL Server throws an error:

Error occurred during query execution:
ConnectorError(ConnectorError { user_facing_error: None, kind: QueryError(TokenError { code: 8003, state: 1, class: 16, message: "The incoming request has too many parameters. The server supports a maximum of 2100 parameters. Reduce the number of parameters and resend the request.", server: "127c67c19c3c", procedure: "", line: 1 }) })

Even when I try to batch fetch them, with skip and take, no matter how small the batch, I get this error.

It seems that under the hood, Prisma translates these relationship queries to a query with thousands of query parameters. I think this should be handled more carefully by the client, because we can't control how Prisma builds the query, and apparently there is no failsafe in this case when using SQL Server.

How to reproduce

  1. Have two models, AddOn and Door
  2. Have a relation from AddOn to Door, called compatibleAddOns
  3. Make 10.000 relations from a single AddOn to 10.000 different Doors
  4. Query a single AddOn:
prismaClient.addOn
    .findUnique({
        where: {
            productId,
        },
        select: {
            compatibleDoors: true
        },
    })

Expected behavior

Return an AddOn with 10.000 compatibleDoors.

Prisma information

model Door {
  id                  String            @id @default(uuid())
  compatibleAddOns    AddOn[]
  ...
}

model AddOn {
  id                      String        @id @default(uuid())
  productId               String        @unique
  compatibleDoors         Door[]
  ...
}
for (let batch = 0; batch < existingRelationshipsCount._count.compatibleDoors; batch += batchSize) {
const existingRelationships = await prismaClient.addOn
    .findUnique({
        where: {
            productId: addOnProductId,
        },
        select: {
            compatibleDoors: {
                take: batchSize,
                skip: batch,
            },
        },
    })
    .catch((e) => {
        console.log('compatibleDoorCount for ERROR:', existingRelationshipsCount._count.compatibleDoors);
        console.log(e);
    });
...
}

Environment & setup

  • OS: macOS
  • Database: SQL Server
  • Node.js version: 16.14.0

Prisma Version

4.8.0
@JaapWeijland JaapWeijland added the kind/bug A reported bug. label Mar 3, 2023
@janpio janpio added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. topic: sql server Microsoft SQL Server team/client Issue for team Client. labels Mar 3, 2023
@JaapWeijland
Copy link
Author

In further research, I found out that I also get this error:

prisma:error The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION

Does the param limit count for all queries within the transaction?

I also noticed that the query that errors out tries to limit the params to 2099. So Im not quite sure why mssql thinks this query contains more than 2100 parameters:

prisma:query SELECT [dbo].[Door].[id] FROM [dbo].[Door] WHERE (1=1 AND [dbo].[Door].[id] IN (@P1,@P2,@P3,@P4,@P5,@P6,@P7,@P8,@P9,@P10,@P11,@P12,@P13,@P14,@P15,@P16,@P17,@P18,@P19,@P20,@P21,@P22,@P23,@P24,@P25,@P26,@P27,@P28,@P29,@P30,@P31,@P32,@P33,@P34,@P35,@P36,@P37,@P38,@P39,@P40,@P41,@P42,@P43,@P44,@P45,@P46,@P47,@P48,@P49,@P50,@P51,@P52,@P53,@P54,@P55,@P56,@P57,@P58,@P59,@P60,@P61,@P62,@P63,@P64,@P65,@P66,@P67,@P68,@P69,@P70,@P71,@P72,@P73,@P74,@P75,@P76,@P77,@P78,@P79,@P80,@P81,@P82,@P83,@P84,@P85,@P86,@P87,@P88,@P89,@P90,@P91,@P92,@P93,@P94,@P95,@P96,@P97,@P98,@P99,@P100,@P101,@P102,@P103,@P104,@P105,@P106,@P107,@P108,@P109,@P110,@P111,@P112,@P113,@P114,@P115,@P116,@P117,@P118,@P119,@P120,@P121,@P122,@P123,@P124,@P125,@P126,@P127,@P128,@P129,@P130,@P131,@P132,@P133,@P134,@P135,@P136,@P137,@P138,@P139,@P140,@P141,@P142,@P143,@P144,@P145,@P146,@P147,@P148,@P149,@P150,@P151,@P152,@P153,@P154,@P155,@P156,@P157,@P158,@P159,@P160,@P161,@P162,@P163,@P164,@P165,@P166,@P167,@P168,@P169,@P170,@P171,@P172,@P173,@P174,@P175,@P176,@P177,@P178,@P179,@P180,@P181,@P182,@P183,@P184,@P185,@P186,@P187,@P188,@P189,@P190,@P191,@P192,@P193,@P194,@P195,@P196,@P197,@P198,@P199,@P200,@P201,@P202,@P203,@P204,@P205,@P206,@P207,@P208,@P209,@P210,@P211,@P212,@P213,@P214,@P215,@P216,@P217,@P218,@P219,@P220,@P221,@P222,@P223,@P224,@P225,@P226,@P227,@P228,@P229,@P230,@P231,@P232,@P233,@P234,@P235,@P236,@P237,@P238,@P239,@P240,@P241,@P242,@P243,@P244,@P245,@P246,@P247,@P248,@P249,@P250,@P251,@P252,@P253,@P254,@P255,@P256,@P257,@P258,@P259,@P260,@P261,@P262,@P263,@P264,@P265,@P266,@P267,@P268,@P269,@P270,@P271,@P272,@P273,@P274,@P275,@P276,@P277,@P278,@P279,@P280,@P281,@P282,@P283,@P284,@P285,@P286,@P287,@P288,@P289,@P290,@P291,@P292,@P293,@P294,@P295,@P296,@P297,@P298,@P299,@P300,@P301,@P302,@P303,@P304,@P305,@P306,@P307,@P308,@P309,@P310,@P311,@P312,@P313,@P314,@P315,@P316,@P317,@P318,@P319,@P320,@P321,@P322,@P323,@P324,@P325,@P326,@P327,@P328,@P329,@P330,@P331,@P332,@P333,@P334,@P335,@P336,@P337,@P338,@P339,@P340,@P341,@P342,@P343,@P344,@P345,@P346,@P347,@P348,@P349,@P350,@P351,@P352,@P353,@P354,@P355,@P356,@P357,@P358,@P359,@P360,@P361,@P362,@P363,@P364,@P365,@P366,@P367,@P368,@P369,@P370,@P371,@P372,@P373,@P374,@P375,@P376,@P377,@P378,@P379,@P380,@P381,@P382,@P383,@P384,@P385,@P386,@P387,@P388,@P389,@P390,@P391,@P392,@P393,@P394,@P395,@P396,@P397,@P398,@P399,@P400,@P401,@P402,@P403,@P404,@P405,@P406,@P407,@P408,@P409,@P410,@P411,@P412,@P413,@P414,@P415,@P416,@P417,@P418,@P419,@P420,@P421,@P422,@P423,@P424,@P425,@P426,@P427,@P428,@P429,@P430,@P431,@P432,@P433,@P434,@P435,@P436,@P437,@P438,@P439,@P440,@P441,@P442,@P443,@P444,@P445,@P446,@P447,@P448,@P449,@P450,@P451,@P452,@P453,@P454,@P455,@P456,@P457,@P458,@P459,@P460,@P461,@P462,@P463,@P464,@P465,@P466,@P467,@P468,@P469,@P470,@P471,@P472,@P473,@P474,@P475,@P476,@P477,@P478,@P479,@P480,@P481,@P482,@P483,@P484,@P485,@P486,@P487,@P488,@P489,@P490,@P491,@P492,@P493,@P494,@P495,@P496,@P497,@P498,@P499,@P500,@P501,@P502,@P503,@P504,@P505,@P506,@P507,@P508,@P509,@P510,@P511,@P512,@P513,@P514,@P515,@P516,@P517,@P518,@P519,@P520,@P521,@P522,@P523,@P524,@P525,@P526,@P527,@P528,@P529,@P530,@P531,@P532,@P533,@P534,@P535,@P536,@P537,@P538,@P539,@P540,@P541,@P542,@P543,@P544,@P545,@P546,@P547,@P548,@P549,@P550,@P551,@P552,@P553,@P554,@P555,@P556,@P557,@P558,@P559,@P560,@P561,@P562,@P563,@P564,@P565,@P566,@P567,@P568,@P569,@P570,@P571,@P572,@P573,@P574,@P575,@P576,@P577,@P578,@P579,@P580,@P581,@P582,@P583,@P584,@P585,@P586,@P587,@P588,@P589,@P590,@P591,@P592,@P593,@P594,@P595,@P596,@P597,@P598,@P599,@P600,@P601,@P602,@P603,@P604,@P605,@P606,@P607,@P608,@P609,@P610,@P611,@P612,@P613,@P614,@P615,@P616,@P617,@P618,@P619,@P620,@P621,@P622,@P623,@P624,@P625,@P626,@P627,@P628,@P629,@P630,@P631,@P632,@P633,@P634,@P635,@P636,@P637,@P638,@P639,@P640,@P641,@P642,@P643,@P644,@P645,@P646,@P647,@P648,@P649,@P650,@P651,@P652,@P653,@P654,@P655,@P656,@P657,@P658,@P659,@P660,@P661,@P662,@P663,@P664,@P665,@P666,@P667,@P668,@P669,@P670,@P671,@P672,@P673,@P674,@P675,@P676,@P677,@P678,@P679,@P680,@P681,@P682,@P683,@P684,@P685,@P686,@P687,@P688,@P689,@P690,@P691,@P692,@P693,@P694,@P695,@P696,@P697,@P698,@P699,@P700,@P701,@P702,@P703,@P704,@P705,@P706,@P707,@P708,@P709,@P710,@P711,@P712,@P713,@P714,@P715,@P716,@P717,@P718,@P719,@P720,@P721,@P722,@P723,@P724,@P725,@P726,@P727,@P728,@P729,@P730,@P731,@P732,@P733,@P734,@P735,@P736,@P737,@P738,@P739,@P740,@P741,@P742,@P743,@P744,@P745,@P746,@P747,@P748,@P749,@P750,@P751,@P752,@P753,@P754,@P755,@P756,@P757,@P758,@P759,@P760,@P761,@P762,@P763,@P764,@P765,@P766,@P767,@P768,@P769,@P770,@P771,@P772,@P773,@P774,@P775,@P776,@P777,@P778,@P779,@P780,@P781,@P782,@P783,@P784,@P785,@P786,@P787,@P788,@P789,@P790,@P791,@P792,@P793,@P794,@P795,@P796,@P797,@P798,@P799,@P800,@P801,@P802,@P803,@P804,@P805,@P806,@P807,@P808,@P809,@P810,@P811,@P812,@P813,@P814,@P815,@P816,@P817,@P818,@P819,@P820,@P821,@P822,@P823,@P824,@P825,@P826,@P827,@P828,@P829,@P830,@P831,@P832,@P833,@P834,@P835,@P836,@P837,@P838,@P839,@P840,@P841,@P842,@P843,@P844,@P845,@P846,@P847,@P848,@P849,@P850,@P851,@P852,@P853,@P854,@P855,@P856,@P857,@P858,@P859,@P860,@P861,@P862,@P863,@P864,@P865,@P866,@P867,@P868,@P869,@P870,@P871,@P872,@P873,@P874,@P875,@P876,@P877,@P878,@P879,@P880,@P881,@P882,@P883,@P884,@P885,@P886,@P887,@P888,@P889,@P890,@P891,@P892,@P893,@P894,@P895,@P896,@P897,@P898,@P899,@P900,@P901,@P902,@P903,@P904,@P905,@P906,@P907,@P908,@P909,@P910,@P911,@P912,@P913,@P914,@P915,@P916,@P917,@P918,@P919,@P920,@P921,@P922,@P923,@P924,@P925,@P926,@P927,@P928,@P929,@P930,@P931,@P932,@P933,@P934,@P935,@P936,@P937,@P938,@P939,@P940,@P941,@P942,@P943,@P944,@P945,@P946,@P947,@P948,@P949,@P950,@P951,@P952,@P953,@P954,@P955,@P956,@P957,@P958,@P959,@P960,@P961,@P962,@P963,@P964,@P965,@P966,@P967,@P968,@P969,@P970,@P971,@P972,@P973,@P974,@P975,@P976,@P977,@P978,@P979,@P980,@P981,@P982,@P983,@P984,@P985,@P986,@P987,@P988,@P989,@P990,@P991,@P992,@P993,@P994,@P995,@P996,@P997,@P998,@P999,@P1000,@P1001,@P1002,@P1003,@P1004,@P1005,@P1006,@P1007,@P1008,@P1009,@P1010,@P1011,@P1012,@P1013,@P1014,@P1015,@P1016,@P1017,@P1018,@P1019,@P1020,@P1021,@P1022,@P1023,@P1024,@P1025,@P1026,@P1027,@P1028,@P1029,@P1030,@P1031,@P1032,@P1033,@P1034,@P1035,@P1036,@P1037,@P1038,@P1039,@P1040,@P1041,@P1042,@P1043,@P1044,@P1045,@P1046,@P1047,@P1048,@P1049,@P1050,@P1051,@P1052,@P1053,@P1054,@P1055,@P1056,@P1057,@P1058,@P1059,@P1060,@P1061,@P1062,@P1063,@P1064,@P1065,@P1066,@P1067,@P1068,@P1069,@P1070,@P1071,@P1072,@P1073,@P1074,@P1075,@P1076,@P1077,@P1078,@P1079,@P1080,@P1081,@P1082,@P1083,@P1084,@P1085,@P1086,@P1087,@P1088,@P1089,@P1090,@P1091,@P1092,@P1093,@P1094,@P1095,@P1096,@P1097,@P1098,@P1099,@P1100,@P1101,@P1102,@P1103,@P1104,@P1105,@P1106,@P1107,@P1108,@P1109,@P1110,@P1111,@P1112,@P1113,@P1114,@P1115,@P1116,@P1117,@P1118,@P1119,@P1120,@P1121,@P1122,@P1123,@P1124,@P1125,@P1126,@P1127,@P1128,@P1129,@P1130,@P1131,@P1132,@P1133,@P1134,@P1135,@P1136,@P1137,@P1138,@P1139,@P1140,@P1141,@P1142,@P1143,@P1144,@P1145,@P1146,@P1147,@P1148,@P1149,@P1150,@P1151,@P1152,@P1153,@P1154,@P1155,@P1156,@P1157,@P1158,@P1159,@P1160,@P1161,@P1162,@P1163,@P1164,@P1165,@P1166,@P1167,@P1168,@P1169,@P1170,@P1171,@P1172,@P1173,@P1174,@P1175,@P1176,@P1177,@P1178,@P1179,@P1180,@P1181,@P1182,@P1183,@P1184,@P1185,@P1186,@P1187,@P1188,@P1189,@P1190,@P1191,@P1192,@P1193,@P1194,@P1195,@P1196,@P1197,@P1198,@P1199,@P1200,@P1201,@P1202,@P1203,@P1204,@P1205,@P1206,@P1207,@P1208,@P1209,@P1210,@P1211,@P1212,@P1213,@P1214,@P1215,@P1216,@P1217,@P1218,@P1219,@P1220,@P1221,@P1222,@P1223,@P1224,@P1225,@P1226,@P1227,@P1228,@P1229,@P1230,@P1231,@P1232,@P1233,@P1234,@P1235,@P1236,@P1237,@P1238,@P1239,@P1240,@P1241,@P1242,@P1243,@P1244,@P1245,@P1246,@P1247,@P1248,@P1249,@P1250,@P1251,@P1252,@P1253,@P1254,@P1255,@P1256,@P1257,@P1258,@P1259,@P1260,@P1261,@P1262,@P1263,@P1264,@P1265,@P1266,@P1267,@P1268,@P1269,@P1270,@P1271,@P1272,@P1273,@P1274,@P1275,@P1276,@P1277,@P1278,@P1279,@P1280,@P1281,@P1282,@P1283,@P1284,@P1285,@P1286,@P1287,@P1288,@P1289,@P1290,@P1291,@P1292,@P1293,@P1294,@P1295,@P1296,@P1297,@P1298,@P1299,@P1300,@P1301,@P1302,@P1303,@P1304,@P1305,@P1306,@P1307,@P1308,@P1309,@P1310,@P1311,@P1312,@P1313,@P1314,@P1315,@P1316,@P1317,@P1318,@P1319,@P1320,@P1321,@P1322,@P1323,@P1324,@P1325,@P1326,@P1327,@P1328,@P1329,@P1330,@P1331,@P1332,@P1333,@P1334,@P1335,@P1336,@P1337,@P1338,@P1339,@P1340,@P1341,@P1342,@P1343,@P1344,@P1345,@P1346,@P1347,@P1348,@P1349,@P1350,@P1351,@P1352,@P1353,@P1354,@P1355,@P1356,@P1357,@P1358,@P1359,@P1360,@P1361,@P1362,@P1363,@P1364,@P1365,@P1366,@P1367,@P1368,@P1369,@P1370,@P1371,@P1372,@P1373,@P1374,@P1375,@P1376,@P1377,@P1378,@P1379,@P1380,@P1381,@P1382,@P1383,@P1384,@P1385,@P1386,@P1387,@P1388,@P1389,@P1390,@P1391,@P1392,@P1393,@P1394,@P1395,@P1396,@P1397,@P1398,@P1399,@P1400,@P1401,@P1402,@P1403,@P1404,@P1405,@P1406,@P1407,@P1408,@P1409,@P1410,@P1411,@P1412,@P1413,@P1414,@P1415,@P1416,@P1417,@P1418,@P1419,@P1420,@P1421,@P1422,@P1423,@P1424,@P1425,@P1426,@P1427,@P1428,@P1429,@P1430,@P1431,@P1432,@P1433,@P1434,@P1435,@P1436,@P1437,@P1438,@P1439,@P1440,@P1441,@P1442,@P1443,@P1444,@P1445,@P1446,@P1447,@P1448,@P1449,@P1450,@P1451,@P1452,@P1453,@P1454,@P1455,@P1456,@P1457,@P1458,@P1459,@P1460,@P1461,@P1462,@P1463,@P1464,@P1465,@P1466,@P1467,@P1468,@P1469,@P1470,@P1471,@P1472,@P1473,@P1474,@P1475,@P1476,@P1477,@P1478,@P1479,@P1480,@P1481,@P1482,@P1483,@P1484,@P1485,@P1486,@P1487,@P1488,@P1489,@P1490,@P1491,@P1492,@P1493,@P1494,@P1495,@P1496,@P1497,@P1498,@P1499,@P1500,@P1501,@P1502,@P1503,@P1504,@P1505,@P1506,@P1507,@P1508,@P1509,@P1510,@P1511,@P1512,@P1513,@P1514,@P1515,@P1516,@P1517,@P1518,@P1519,@P1520,@P1521,@P1522,@P1523,@P1524,@P1525,@P1526,@P1527,@P1528,@P1529,@P1530,@P1531,@P1532,@P1533,@P1534,@P1535,@P1536,@P1537,@P1538,@P1539,@P1540,@P1541,@P1542,@P1543,@P1544,@P1545,@P1546,@P1547,@P1548,@P1549,@P1550,@P1551,@P1552,@P1553,@P1554,@P1555,@P1556,@P1557,@P1558,@P1559,@P1560,@P1561,@P1562,@P1563,@P1564,@P1565,@P1566,@P1567,@P1568,@P1569,@P1570,@P1571,@P1572,@P1573,@P1574,@P1575,@P1576,@P1577,@P1578,@P1579,@P1580,@P1581,@P1582,@P1583,@P1584,@P1585,@P1586,@P1587,@P1588,@P1589,@P1590,@P1591,@P1592,@P1593,@P1594,@P1595,@P1596,@P1597,@P1598,@P1599,@P1600,@P1601,@P1602,@P1603,@P1604,@P1605,@P1606,@P1607,@P1608,@P1609,@P1610,@P1611,@P1612,@P1613,@P1614,@P1615,@P1616,@P1617,@P1618,@P1619,@P1620,@P1621,@P1622,@P1623,@P1624,@P1625,@P1626,@P1627,@P1628,@P1629,@P1630,@P1631,@P1632,@P1633,@P1634,@P1635,@P1636,@P1637,@P1638,@P1639,@P1640,@P1641,@P1642,@P1643,@P1644,@P1645,@P1646,@P1647,@P1648,@P1649,@P1650,@P1651,@P1652,@P1653,@P1654,@P1655,@P1656,@P1657,@P1658,@P1659,@P1660,@P1661,@P1662,@P1663,@P1664,@P1665,@P1666,@P1667,@P1668,@P1669,@P1670,@P1671,@P1672,@P1673,@P1674,@P1675,@P1676,@P1677,@P1678,@P1679,@P1680,@P1681,@P1682,@P1683,@P1684,@P1685,@P1686,@P1687,@P1688,@P1689,@P1690,@P1691,@P1692,@P1693,@P1694,@P1695,@P1696,@P1697,@P1698,@P1699,@P1700,@P1701,@P1702,@P1703,@P1704,@P1705,@P1706,@P1707,@P1708,@P1709,@P1710,@P1711,@P1712,@P1713,@P1714,@P1715,@P1716,@P1717,@P1718,@P1719,@P1720,@P1721,@P1722,@P1723,@P1724,@P1725,@P1726,@P1727,@P1728,@P1729,@P1730,@P1731,@P1732,@P1733,@P1734,@P1735,@P1736,@P1737,@P1738,@P1739,@P1740,@P1741,@P1742,@P1743,@P1744,@P1745,@P1746,@P1747,@P1748,@P1749,@P1750,@P1751,@P1752,@P1753,@P1754,@P1755,@P1756,@P1757,@P1758,@P1759,@P1760,@P1761,@P1762,@P1763,@P1764,@P1765,@P1766,@P1767,@P1768,@P1769,@P1770,@P1771,@P1772,@P1773,@P1774,@P1775,@P1776,@P1777,@P1778,@P1779,@P1780,@P1781,@P1782,@P1783,@P1784,@P1785,@P1786,@P1787,@P1788,@P1789,@P1790,@P1791,@P1792,@P1793,@P1794,@P1795,@P1796,@P1797,@P1798,@P1799,@P1800,@P1801,@P1802,@P1803,@P1804,@P1805,@P1806,@P1807,@P1808,@P1809,@P1810,@P1811,@P1812,@P1813,@P1814,@P1815,@P1816,@P1817,@P1818,@P1819,@P1820,@P1821,@P1822,@P1823,@P1824,@P1825,@P1826,@P1827,@P1828,@P1829,@P1830,@P1831,@P1832,@P1833,@P1834,@P1835,@P1836,@P1837,@P1838,@P1839,@P1840,@P1841,@P1842,@P1843,@P1844,@P1845,@P1846,@P1847,@P1848,@P1849,@P1850,@P1851,@P1852,@P1853,@P1854,@P1855,@P1856,@P1857,@P1858,@P1859,@P1860,@P1861,@P1862,@P1863,@P1864,@P1865,@P1866,@P1867,@P1868,@P1869,@P1870,@P1871,@P1872,@P1873,@P1874,@P1875,@P1876,@P1877,@P1878,@P1879,@P1880,@P1881,@P1882,@P1883,@P1884,@P1885,@P1886,@P1887,@P1888,@P1889,@P1890,@P1891,@P1892,@P1893,@P1894,@P1895,@P1896,@P1897,@P1898,@P1899,@P1900,@P1901,@P1902,@P1903,@P1904,@P1905,@P1906,@P1907,@P1908,@P1909,@P1910,@P1911,@P1912,@P1913,@P1914,@P1915,@P1916,@P1917,@P1918,@P1919,@P1920,@P1921,@P1922,@P1923,@P1924,@P1925,@P1926,@P1927,@P1928,@P1929,@P1930,@P1931,@P1932,@P1933,@P1934,@P1935,@P1936,@P1937,@P1938,@P1939,@P1940,@P1941,@P1942,@P1943,@P1944,@P1945,@P1946,@P1947,@P1948,@P1949,@P1950,@P1951,@P1952,@P1953,@P1954,@P1955,@P1956,@P1957,@P1958,@P1959,@P1960,@P1961,@P1962,@P1963,@P1964,@P1965,@P1966,@P1967,@P1968,@P1969,@P1970,@P1971,@P1972,@P1973,@P1974,@P1975,@P1976,@P1977,@P1978,@P1979,@P1980,@P1981,@P1982,@P1983,@P1984,@P1985,@P1986,@P1987,@P1988,@P1989,@P1990,@P1991,@P1992,@P1993,@P1994,@P1995,@P1996,@P1997,@P1998,@P1999,@P2000,@P2001,@P2002,@P2003,@P2004,@P2005,@P2006,@P2007,@P2008,@P2009,@P2010,@P2011,@P2012,@P2013,@P2014,@P2015,@P2016,@P2017,@P2018,@P2019,@P2020,@P2021,@P2022,@P2023,@P2024,@P2025,@P2026,@P2027,@P2028,@P2029,@P2030,@P2031,@P2032,@P2033,@P2034,@P2035,@P2036,@P2037,@P2038,@P2039,@P2040,@P2041,@P2042,@P2043,@P2044,@P2045,@P2046,@P2047,@P2048,@P2049,@P2050,@P2051,@P2052,@P2053,@P2054,@P2055,@P2056,@P2057,@P2058,@P2059,@P2060,@P2061,@P2062,@P2063,@P2064,@P2065,@P2066,@P2067,@P2068,@P2069,@P2070,@P2071,@P2072,@P2073,@P2074,@P2075,@P2076,@P2077,@P2078,@P2079,@P2080,@P2081,@P2082,@P2083,@P2084,@P2085,@P2086,@P2087,@P2088,@P2089,@P2090,@P2091,@P2092,@P2093,@P2094,@P2095,@P2096,@P2097,@P2098,@P2099))

@joshkay
Copy link

joshkay commented Mar 20, 2023

I'm running into the same issue.

@mhesham93
Copy link

same issue here

@thugzook
Copy link

thugzook commented May 3, 2023

Assertion violation on the database: too many bind variables in prepared statement, expected maximum of 32767, received 33023

On my end. v4.4.0, 4.12.0, v4.1.0

@janpio
Copy link
Member

janpio commented May 17, 2023

Can you help us to get to a reproduction (for SQL Server)? Any minimal project that leads to this behavior would help, so we can reproduce, debug and hopefully fix. Thanks.

@janpio janpio changed the title The incoming request has too many parameters. The server supports a maximum of 2100 parameters. Reduce the number of parameters and resend the request. SQL Server: The incoming request has too many parameters. The server supports a maximum of 2100 parameters. Reduce the number of parameters and resend the request. May 17, 2023
@JaapWeijland
Copy link
Author

Hi @janpio, I was able to reproduce this error in isolation. You can reproduce this yourself by cloning and running the project within the following repo:

https://github.com/JaapWeijland/prisma-mssql-issue

The case is a bit different than I initially explained, but I was able to reproduce the same error using a smaller conceptual domain nonetheless.

Happy to help. If there are any questions, please let me know.

@jonalport
Copy link

jonalport commented May 19, 2023

Hi. I'm getting this too in production code. We've batched the createManys into a transaction so each one only creates 1000 entities at at a time, but I still get it for some tables. I seem to also get it on a findMany sometimes, have removed relationships from the query in an attempt to get them to run more successfully.
Using v4.11.0

Prisma seems to manage the number of parameters to stay just below 2100, for example this one with 2094:

https://gist.github.com/jonalport/9d4e594780a85f05d03eef6aabaaac47

But perhaps there is a mistake in certain scenarios where it goes over?


Error example:
image

@janpio janpio added bug/2-confirmed Bug has been reproduced and confirmed. and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels Jun 5, 2023
@janpio
Copy link
Member

janpio commented Jun 5, 2023

Amazing @JaapWeijland, I can reproduce with this:

gitpod /workspace/prisma-mssql-issue (master) $ npx ts-node src/index.ts
Deleting all...
Seeding database...
Deleting relations...
/workspace/prisma-mssql-issue/node_modules/@prisma/client/runtime/library.js:171
`)}function rd(e){return Array.isArray(e)}var Hi=(e,t)=>([r,...n])=>{l...turn t.map(El);default:return t}}var Ml=R(_r());var Tl=["datasources","errorFormat","log","__internal","rejectOnNotFound"],vl=["pretty","colorless","minimal"],Pl=["info","query","warn","error"],ud={datasources:(e,t)=>{if(!!e){if(typeof e!="object"||Array.isArray(e))throw new z(`Invalid value ${JSON.stringify(e)} for "datasources" provided to PrismaClient constructor`);for(let[r,n]of Object.entries(e)){if(!t.includes(r)){let i=At(r,t)||`Available datasources: ${t.join(", ")}`;throw new z(`Unknown datasource ${r} provided to PrismaClient constructor.${i}`)}if(typeof n!="object"||Array.isArray(n))throw new z(`Invalid value ${JSON.stringify(e)} for datasource "${r}" provided to PrismaClient constructor.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              ^
PrismaClientUnknownRequestError: 
Invalid `prisma.door.update()` invocation in
/workspace/prisma-mssql-issue/src/index.ts:44:21

  41 const deleteRelations = async () => {
  42   console.log("Deleting relations...");
  43 
→ 44   await prisma.door.update(
Error occurred during query execution:
ConnectorError(ConnectorError { user_facing_error: None, kind: QueryError(TokenError { code: 8003, state: 1, class: 16, message: "The incoming request has too many parameters. The server supports a maximum of 2100 parameters. Reduce the number of parameters and resend the request.", server: "487897f96434", procedure: "", line: 1 }), transient: false })
    at Pn.handleRequestError (/workspace/prisma-mssql-issue/node_modules/@prisma/client/runtime/library.js:171:7103)
    at Pn.handleAndLogRequestError (/workspace/prisma-mssql-issue/node_modules/@prisma/client/runtime/library.js:171:6358)
    at Pn.request (/workspace/prisma-mssql-issue/node_modules/@prisma/client/runtime/library.js:171:6237) {
  clientVersion: '4.14.1'
}

@janpio
Copy link
Member

janpio commented Jun 5, 2023

Everyone else who posted here (@jonalport @joshkay @mhesham93 @thugzook), can you please share the queries where you experienced this? The current theory is that it has to do with doing things one level deep in the query vs. on the top level. Example from @JaapWeijland:

  await prisma.door.update({
    where: { id: "door-1" },
    data: { addOns: { set: [] } },
                         ^^^^^^
  });

@mhesham93
Copy link

mhesham93 commented Jun 5, 2023

Everyone else who posted here (@jonalport @joshkay @mhesham93 @thugzook), can you please share the queries where you experienced this? The current theory is that it has to do with doing things one level deep in the query vs. on the top level. Example from @JaapWeijland:

  await prisma.door.update({
    where: { id: "door-1" },
    data: { addOns: { set: [] } },
                         ^^^^^^
  });

From my personal experience, when I use wider date range, I start seeing the error otherwise it works fine. here's my query (for example if I wanted to generate data for a week, it would work just fine however if I tried to run the same query to pull data for a month it won't work as expected)

 this.prisma.delivery.findMany({
          orderBy: {
            Date: 'desc',
          },
          where: {
            IsDeleted: false,
            Date:
              getContractorDeliveryReportDto.startDate &&
              getContractorDeliveryReportDto.endDate
                ? {
                    gte: parseServerDate(
                      new Date(getContractorDeliveryReportDto.startDate), <----- this filter
                    ),
                    lte: parseServerDate(
                      new Date(getContractorDeliveryReportDto.endDate), <---- this filter
                    ),
                  }
                : {},
          ... rest of the query here
        }),

@thugzook
Copy link

thugzook commented Jun 5, 2023

Everyone else who posted here (@jonalport @joshkay @mhesham93 @thugzook), can you please share the queries where you experienced this? The current theory is that it has to do with doing things one level deep in the query vs. on the top level. Example from @JaapWeijland:

  await prisma.door.update({
    where: { id: "door-1" },
    data: { addOns: { set: [] } },
                         ^^^^^^
  });

Not sure if the issue is related to that, here's my query that I had to refactor to using cursor to work properly.

prisma.token.findMany
          where: {
            intA,
            OR: [
              {
                boolA: true,
              },
              {
                stringA: {
                  not: "0",
                },
              },
              {
                stringArrayA: {
                  isEmpty: false,
                },
              },
            ],
          },
          orderBy: [{ stringB: "asc" }],
          select: {
            stringA: true,
           ...
            boolA: true,
           ...
            objectA: {
              select: {
                intA: true,
                ...
              },

@pmosconi
Copy link

AFAIK the issue is not unique to MSSQL, although the parameter number message error probably is.
Prisma is not translating the query code into a SQL JOIN query, but it is making multiple cascading queries and finally joining results in code within the driver: one can easily see this by activating query logging in the client:
new PrismaClient({ log: ['query'] })

This behavior is far from optimal in any situation where the database is non trivial:

  • missing indexes
  • too much network traffic
  • too many parameters error
  • api timeouts

It usually ends up in unexpected errors when the system is live and the data grow over a certain point: I've seen this happening in both MySql and MSSql (not because other databases are immune, rather because these are the ones I routinely use).

While I firmly believe that Prisma is generally superior, in this one aspect it is still trailing way behind Sequelize whose join queries are excellent.

The workaround I usually adopt for the typical application where you have a list of rows and then you do CRUD operations on the single record, is to create views for the lists, add them as entities to schema and perform the findMany operation on them.
Pros:

  • the views underlying queries are natively written in sql, they can be optimized and use the appropriate indexes
  • the queries will return only the needed fields
  • findMany instructions become extremely simple to write and test

Con:

  • new fake entities are added to the schema and manual work isrequired; this is true both for schema first and code first approaches
  • there is no easy way to automatically apply these kind of migrations to the database

I believe this is a good compromise, but I'd really like to know when the Prisma team plans to fix this problem.

@janpio
Copy link
Member

janpio commented Jun 16, 2023

This issue here is specifically about the SQL Server problem, which should not happen even with our current logic. For more general requests about changing our joining behavior, you can for example see #5184

@dmitri-gb
Copy link

I believe this issue is due to the default_batch_size of 2099. I'm no expert in SQL Server internals, but according to this SO answer, the true limit should be 2098.

It's possible to use the environment variable QUERY_BATCH_SIZE=2098 as a workaround.

@cmoscosoz
Copy link

cmoscosoz commented Jul 3, 2023

Same issue here.
I had to do the joint using prisma.$queryRaw to make it worked

@agingerbreadman
Copy link

agingerbreadman commented Sep 21, 2023

Any progress with this one?
I have the exact same problem. Tried to workaround it by fetching the relevant IDs of a single table, and manually chunking queries to the relating tables. Seems kinda anti pattern.. any suggestions perhaps?

@seed-of-apricot
Copy link

I believe this issue is due to the default_batch_size of 2099. I'm no expert in SQL Server internals, but according to this SO answer, the true limit should be 2098.

It's possible to use the environment variable QUERY_BATCH_SIZE=2098 as a workaround.

This worked for me, thank you @dmitri-gb.

@janpio
Copy link
Member

janpio commented Feb 20, 2024

Thanks for the comments and investigation here, I can confirm that in a reproduction lowering the value to 2098 via an environment variable indeed works in certain situations.

@thedrewguy
Copy link

Setting the batch size to 2098 will work in some cases, but does not solve the root problem.
This is because it is possible to add more parameters to the query.

A fixed batch size is a fundamentally flawed approach, and the batch size should instead be dynamically determined based on the number of other parameters.

For example, if batch size is 2098 and you wanted to get all compatible red doors:

prisma.findUnique({
    where: {
        productId,
    },
    select: {
        compatibleDoors: true,
        where: {
            color: 'red'
        }
    },
})

For your 'compatible doors' batched query, you'll end up with sql something like this:

exec sp_executesql 
N'select * from Doors where color = @P1 and id in(@P2, ... @P2199)'
, N'@P1 varchar(255) ... @P2199 int'
, @P1='red', ... @P2199=12345

Here you have sp_executesql with 2101 parameters:

  • 1 sql string
  • 1 parameter list
  • 1 color
  • 2098 ids

As a workaround I've set QUERY_BATCH_SIZE=2000 in my .env

@jlkravitz
Copy link

jlkravitz commented Feb 20, 2024 via email

@janpio
Copy link
Member

janpio commented Feb 20, 2024

Of course @thedrewguy, but using the environment variable confirms what the problem is (the automatically used number to calculate when and how to chunk queries). Lowering the number by 1 will already improve the situation, before we are able to properly fix this logic.

Any suggestions or optimally examples for such logic that can handle this?

@thedrewguy
Copy link

thedrewguy commented Feb 21, 2024

This is definitely a good step and I'm sure it'll fix most cases!
Very happy to see this getting some action.

I've seen other ORMs do this without parameterizing the "join" field (just a comma-separated list) but this leaves you open to sql injection.

There are two ways I could see to solve this:

1. Keep query batching but limit batch size to fix the parameter issue

you'd have a MAX_BATCH_SIZE constant instead of QUERY_BATCH_SIZE, default 2098
Then you would work out the number of additional parameters (in the example above, 1 for color) and subtract that from the max.
For the query you would use:
batchSize = MAX_BATCH_SIZE - numAdditionalParameters

2. Do away with query batching and use temp tables to temporarily store join fields - my personal dream

I would guess this is a much more performant option for large datasets and it's injection safe
Downsides are write operations and more complexity.

Say you have a prisma query:

prisma.person.findMany({
  select: {
    emailAddresses: true,
  },
});

you could aim for sql like this.
Person query:

create table #Main(id int primary key, FirstName nvarchar(255), LastName nvarchar(255))

insert #Main
exec sp_executesql N'select PeopleID, FirstName, LastName from People'

create table #Join(id int)
create unique clustered index ix on #Join(id)
insert #Join(id)
select distinct id
from #Main

select * from #Main
drop table #Main

Email query:

select EMailAddress.* from EMailAddress join #Join on EMailAddress.PeopleID = #Join.id
drop table #Join

multi-column relations and temp table indexing are important considerations for this approach

@janpio janpio changed the title SQL Server: The incoming request has too many parameters. The server supports a maximum of 2100 parameters. Reduce the number of parameters and resend the request. SQL Server: The incoming request has too many parameters. The server supports a maximum of 2100 parameters. Reduce the number of parameters and resend the request. Feb 21, 2024
@PatricioNG
Copy link

PatricioNG commented Apr 2, 2024

Posting this without a reproducible example at hand to offer (apologies), but we did run into a situation today where keeping our batch size at 2050 works generally for most queries, but using include on a typical order headers/lines table that had a FK set up did not work and threw errors. We followed the advice to reduce our query size even lower, seems like 1000 finally allows the query to be run, however, curiously when the query runs on a quiet dev DB with no activity aside from my application making this one query against it -> it goes into status: stalled immediately and never recovers/hangs for what seems like forever? (I lost patience after 10 minutes so can't actually confirm if it ever really will recover 🤷‍♂️)

Checking out the process log there are no other blocking queries or activities as I'm the only activity on the DB. For reference if it matters the header table has 12,533,371 rows, the lines has 46,731,122.

(Edit: I should also note we aren't pulling back that many rows, just wanted to note the table size as maybe it's trying to search in an inefficient way? The actual result set we're pulling using a raw query is ~3,700 records as it's filtered on a few different statuses/etc.)

Sadly it seems like the workaround is only a partial workaround 🥲 So it's back to writing some ugly code to split things up and do it manually on our end😅

Hope we have made progress/are closer to a resolve on this one! 🙏🕯️

@pupocalypse
Copy link

Wanted to flag that this issue is also happening in groupBy.

// ✅ runs using the QUERY_BATCH_SIZE in .env
await prisma.orderlines.findMany({
	where: {
		order_number: { in: ordersList.map((order) => order.orderNumber) },
	},
});
// ❌ fails with 'too many params' error despite QUERY_BATCH_SIZE in .env
await prisma.orderlines.groupBy({
	by: ['order_number', 'box_id'],
	where: {
		order_number: { in: ordersList.map((order) => order.orderNumber) },
	},
});

@janpio
Copy link
Member

janpio commented May 14, 2024

I believe this issue is due to the default_batch_size of 2099. I'm no expert in SQL Server internals, but according to this SO answer, the true limit should be 2098.

This has been changed in 5.14.0 today: prisma/prisma-engines#4747
Note that this will only fix this problem in the most simple cases, and there is more work to be done to properly chunk queries of even the slightest complexity. But that was a simple start at least.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: chunking topic: sql server Microsoft SQL Server
Projects
None yet
Development

No branches or pull requests