diff --git a/lapis/db/model/relations.lua b/lapis/db/model/relations.lua index a183a977..7060a128 100644 --- a/lapis/db/model/relations.lua +++ b/lapis/db/model/relations.lua @@ -509,7 +509,11 @@ has_many = function(self, name, opts) table.insert(additional_clause, where) else for k, v in pairs(where) do - additional_clause[k] = v + if type(k) == "number" then + table.insert(additional_clause, v) + else + additional_clause[k] = v + end end end end @@ -526,7 +530,11 @@ has_many = function(self, name, opts) table.insert(additional_clause, more_where) else for k, v in pairs(more_where) do - additional_clause[k] = v + if type(k) == "number" then + table.insert(additional_clause, v) + else + additional_clause[k] = v + end end end end diff --git a/lapis/db/model/relations.moon b/lapis/db/model/relations.moon index 89840eb8..34acd37b 100644 --- a/lapis/db/model/relations.moon +++ b/lapis/db/model/relations.moon @@ -400,7 +400,10 @@ has_many = (name, opts) => table.insert additional_clause, where else for k,v in pairs where - additional_clause[k] = v + if type(k) == "number" -- append array clauses + table.insert additional_clause, v + else + additional_clause[k] = v if more_where = calling_opts and calling_opts.where additional_clause = { @@db.clause clause } unless additional_clause @@ -409,7 +412,10 @@ has_many = (name, opts) => table.insert additional_clause, more_where else for k,v in pairs more_where - additional_clause[k] = v + if type(k) == "number" -- append array clauses + table.insert additional_clause, v + else + additional_clause[k] = v if additional_clause and next additional_clause clause = @@db.clause additional_clause diff --git a/spec/relations_spec.moon b/spec/relations_spec.moon index d56b3e12..6fcd6154 100644 --- a/spec/relations_spec.moon +++ b/spec/relations_spec.moon @@ -536,18 +536,27 @@ describe "lapis.db.model.relations", -> it "make has_many getter", -> models.Posts = class extends Model + models.Things = class extends Model + models.Users = class extends Model @relations: { {"posts", has_many: "Posts"} {"more_posts", has_many: "Posts", where: {color: "blue"}} {"fresh_posts", has_many: "Posts", order: "id desc"} - } + {"with_key", has_many: "Things", key: "object_id", order: "id desc"} + + {"with_key_where", has_many: "Things", key: "object_id", where: { + {"object_type = ?", 1} + }, order: "id desc"} + } assert_queries { [[SELECT * FROM "posts" WHERE "user_id" = 1234]] [[SELECT * FROM "posts" WHERE "user_id" = 1234 AND "color" = 'blue']] [[SELECT * FROM "posts" WHERE "user_id" = 1234 ORDER BY id desc]] + [[SELECT * FROM "things" WHERE "object_id" = 1234 ORDER BY id desc]] + [[SELECT * FROM "things" WHERE "object_id" = 1234 AND (object_type = 1) ORDER BY id desc]] }, -> user = models.Users\load id: 1234 @@ -557,6 +566,9 @@ describe "lapis.db.model.relations", -> user\get_more_posts! user\get_fresh_posts! + user\get_with_key! + user\get_with_key_where! + it "makes has many with db.clause", -> models.Posts = class extends Model models.Users = class extends Model