From 66a494cc0b395bd2a50fccbe335abf4ba8345ab8 Mon Sep 17 00:00:00 2001 From: Madeorsk Date: Tue, 26 Nov 2024 10:46:03 +0100 Subject: [PATCH] Fix keys in many relation query through a pivot table. --- src/relations.zig | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/relations.zig b/src/relations.zig index 18f5d7b..590eb30 100644 --- a/src/relations.zig +++ b/src/relations.zig @@ -64,6 +64,7 @@ pub fn typedMany( .direct => |direct| if (direct.modelKey) |_modelKey| _modelKey else fromRepositoryConfig.key[0], .through => |through| if (through.modelKey) |_modelKey| _modelKey else fromRepositoryConfig.key[0], }; + _ = modelKey; const FromKeyType = std.meta.fields(FromModel)[std.meta.fieldIndex(FromModel, fromRepositoryConfig.key[0]).?].type; const QueryType = _query.RepositoryQuery(ToModel, ToTable, toRepositoryConfig, null, struct { @@ -112,18 +113,18 @@ pub fn typedMany( .through => |through| { // Add SELECT. query.select(.{ - .sql = baseSelect ++ ", \"" ++ prefix ++ "pivot" ++ "\".\"" ++ through.joinForeignKey ++ "\" AS \"__zrm_relation_key\"", + .sql = baseSelect ++ ", \"" ++ prefix ++ "pivot" ++ "\".\"" ++ through.joinModelKey ++ "\" AS \"__zrm_relation_key\"", .params = &[0]_sql.RawQueryParameter{}, }); query.join(.{ .sql = "INNER JOIN \"" ++ through.table ++ "\" AS \"" ++ prefix ++ "pivot" ++ "\" " ++ - "ON \"" ++ toRepositoryConfig.table ++ "\"." ++ modelKey ++ " = " ++ "\"" ++ prefix ++ "pivot" ++ "\"." ++ through.joinModelKey, + "ON \"" ++ toRepositoryConfig.table ++ "\"." ++ foreignKey ++ " = " ++ "\"" ++ prefix ++ "pivot" ++ "\"." ++ through.joinForeignKey, .params = &[0]_sql.RawQueryParameter{}, }); // Build WHERE condition. - try query.whereIn(FromKeyType, "\"" ++ prefix ++ "pivot" ++ "\".\"" ++ through.joinForeignKey ++ "\"", modelsIds); + try query.whereIn(FromKeyType, "\"" ++ prefix ++ "pivot" ++ "\".\"" ++ through.joinModelKey ++ "\"", modelsIds); }, }