lapis icon indicating copy to clipboard operation
lapis copied to clipboard

table relation with primary keys and foreign keys

Open ghost opened this issue 3 years ago • 1 comments

Although most of the information provided in the lapis documentation is to operate on a single table, how can I make filters or projections by relaunching tables, for example with the following statement?

SELECT u.id,u.activated,v.expire AS token_expire FROM users AS u INNER JOIN verifications AS v ON u.id = v.id;

a quick way is to usequery (query, params ...) but is there another method?

ghost avatar Apr 15 '21 05:04 ghost

this is my temporal solution

local userModel = {
	users = require("lapis.db.model").Model:extend("users"),
	db    = require("lapis.db")
}

function userModel:findUser(username,email)
	local str_query = "SELECT u.id,u.activated,v.expire AS token_expiration FROM users AS u "..
                          "INNER JOIN verifications AS v ON u.id = v.id "..
                          "WHERE u.username = ? OR u.email = ? LIMIT 2"

	local list_users = self.db.query(str_query,username,email)

	return list_users
end

return setmetatable({}, { __index = userModel })

ghost avatar Apr 15 '21 06:04 ghost

For the time being, lapis will not support using join syntax for preloading relations. You are still welcome to use joins by writing the sql fragments yourself.

leafo avatar Dec 14 '22 22:12 leafo