Skip to content

Filter based on related tables' columns #250

Open
@ligustah

Description

@ligustah

Hello there,

I very much enjoy using kallax, so thanks for that!

I'm currently implementing a schema that looks like this:

type A struct {
	kallax.Model
	ID    int64 `pk:"autoincr"`
	C     *C `fk:",inverse"`
}

type B struct {
	kallax.Model
	ID    int64 `pk:"autoincr"`
	C     *C `fk:",inverse"`
}

type C struct {
	kallax.Model
	ID       int64 `pk:"autoincr"`
	Property int64
}

So there is a 1:1 relationship between A and C, and between B and C respectively.

Now maybe I'm just holding it wrong, but I cannot seem to find a way to query all A's that have a specific value for the Property of their related C. What I was trying to do is this:

aStore := NewAStore(db).Debug()
a, err := aStore.FindOne(
	NewAQuery().
		WithC().
		Where(kallax.Eq(Schema.C.Property, 5)),
)

When I run that, however, I get this behavior:

2017/12/25 11:18:14 kallax: Query: SELECT __a.id, __a.c_id, __c_C.id, __c_C.property FROM a __a LEFT JOIN c __c_C ON (__c_C.id = __a.c_id) WHERE __a.property = $1 LIMIT 1, args: [5]
2017/12/25 11:18:14 pq: column __a.property does not exist

So from what I can tell kallax is using the schema of A to build the qualified column name, which obviously doesn't work. Is there a way in kallax right now, that would allow me to run this query successfully?

Otherwise I was thinking about implementing a wrapper for the SchemaField that would always use a specific base schema, such that Schema.C.Property would always use the schema of C (which I assumed was the default behavior) or modify the code generation to always maintain this mapping.

Thanks in advance, and enjoy your holidays!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions