Genie Discord forum

Author AvatarAlbert35
6/5/2023, 10:24:52 PM

Could anyone give me an exaple on how to use SQLJoin and SQLOn to make a query to join two related tables?

Author AvatarBouzeux
6/6/2023, 3:04:55 PM

I don't think there's a good way to do it right now, but this should work :

function find_join(m,q,j)
    data = DataFrame(m, q, j)
    models = SearchLight.to_models(m, data)
    joined = [copy(r) for r in eachrow(data)]

    models, joined
end

on = SQLOn( SQLColumn("comments.user_id"), SQLColumn("users.id"))
j = Vector{SQLJoin}([
    SQLJoin(Users.User, columns = [SQLColumn("users.name")], [on])
])

where_flag = SQLWhereExpression("$(SQLColumn("comments.flag")) = ?", flag)
q = SQLQuery(where = [SQLWhereExpression("$(SQLColumn(:topic_id)) = ?", topic_id), where_flag], order = SQLOrder(:datetime, "DESC"))

find_join(Comments.Comment, q, j)