-
Hi there, First of all, thanks for this library, it is very useful. I am trying to use TL;DR: I noticed that this post is quite long. I try to briefly pinpoint the issue. I learned how to use aliases to map joins to nested structures. The issue I am having is that I am joining two tables ( I have this schema: CREATE TABLE entity (
id INTEGER,
name TEXT NOT NULL,
PRIMARY KEY(id)
);
CREATE TABLE account (
id INTEGER,
name TEXT NOT NULL,
owner_id INTEGER NOT NULL,
PRIMARY KEY(id),
);
CREATE TABLE "transaction" (
id INTEGER,
from_id INTEGER NOT NULL,
to_id INTEGER NOT NULL,
amount INTEGER NOT NULL,
PRIMARY KEY(id)
); type Entity struct {
ID null.Int `sql:"primary_key"`
Name string
}
type Account struct {
ID null.Int `sql:"primary_key"`
Name string
Owner Entity
}
type Transaction struct {
ID null.Int `sql:"primary_key"`
From Account `alias:"From"`
To Account `alias:"To"`
Amount decimal.Decimal
} To extract all transactions for a certain entity I use the following query: From := j.Account.AS("from")
To := j.Account.AS("to")
FromEntity := j.Entity.AS("from_owner")
ToEntity := j.Entity.AS("to_owner")
stmt := jet.SELECT(
Transaction.AllColumns,
From.AllColumns,
To.AllColumns,
FromEntity.AllColumns,
ToEntity.AllColumns,
).FROM(
Transaction.INNER_JOIN(
From,
From.ID.EQ(Transaction.FromID),
).INNER_JOIN(
To,
To.ID.EQ(Transaction.ToID),
).INNER_JOIN(
FromEntity,
FromEntity.ID.EQ(From.OwnerID),
).INNER_JOIN(
ToEntity,
ToEntity.ID.EQ(To.OwnerID),
),
).WHERE(
FromEntity.ID.EQ(jet.Int(int64(eID))).OR(ToEntity.ID.EQ(jet.Int(int64(eID)))),
)
list := []Transaction{}
err := stmt.Query(db, &list) The query works as expected but the type Account struct {
ID null.Int `sql:"primary_key"`
Name string
Owner Entity `alias:"from_owner"` //but also `alias:"to_owner"` ?
} This works, but of course it fills both owners as the "from" owner. I should also add a second alias From := j.Account.AS("from")
To := j.Account.AS("to")
FromEntity := j.Entity.AS("from.owner")
ToEntity := j.Entity.AS("to.owner") with the part before the dot referring to the parent alias. type Account struct {
ID null.Int `sql:"primary_key"`
Name string
Owner Entity `alias:"owner"`
} Q: How should I modify the aliases and/or the model to have both Thanks a lot. I've been banging the head against the wall for a while now. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Hi, I don't see any other way than to create two type Entity struct {
ID null.Int `sql:"primary_key"`
Name string
}
type ToAccount struct {
ID null.Int `sql:"primary_key"`
Name string
Owner Entity `alias:"to_owner"`
}
type FromAccount struct {
ID null.Int `sql:"primary_key"`
Name string
Owner Entity `alias:"from_owner"`
}
type Transaction struct {
ID null.Int `sql:"primary_key"`
From ToAccount `alias:"From"`
To FromAccount `alias:"To"`
Amount decimal.Decimal
} |
Beta Was this translation helpful? Give feedback.
Hi,
I don't see any other way than to create two
Account
structs: