Omitted schema name in cross-schema queries with sorting
- Dominant language
- Go
- Stars
- 17.2k
- Forks
- 1k
- PR merge metrics
- No merged PRs in 30d
Description
Schema name is omitted in the cross-schema queries with sorting by Edge attributes.
- [x] The issue is present in the latest release.
- [x] I have searched the [issues](https://github.com/ent/ent/issues) of this repository and believe that this is not a duplicate.
## Current Behavior 😯
I have 2 schemas in my PostgreSQL DB: public (books table) and alternative (accounts table).
When I make a query to fetch `books` with `accounts` and sort by account attribute, a built
SQL query omits schema name in `JOIN` section that results in the execution error.
## Expected Behavior 🤔
All table names of my queries should have schema names if they were specified in ent/schema files.
## Steps to Reproduce 🕹
Account entity that belongs to `accounts` table in `alternative` schema:
```go
type Account struct{
ent.Schema
}
func (Account) Annotations() []schema.Annotation{
entsql.SchemaTable("alternative", "accounts"),
entgql.RelayConnection(),
entgql.QueryField(),
}
func (Account) Fields() []schema.Field{
return []ent.Field{
field.String("name").Annotations(entgql.OrderField("NAME"))
}
}
func (Account) Edges() []schema.Edge{
return []ent.Edge{
edge.To("books", Book.Type)
}
}
```
Book entity that belongs to `books` table in `public` schema:
```go
type Book struct{
ent.Schema
}
func (Book) Annotations() []schema.Annotation{
entsql.SchemaTable("public", "books"),
entgql.RelayConnection(),
entgql.QueryField(),
}
func (Book) Fields() []schema.Field{
return []ent.Field{
field.String("name").Annotations(entgql.OrderField("NAME")),
field.Float("price").Annotations(entgql.OrderField("PRICE"))
field.Int("account_id").Annotations(entgql.OrderField("ACCOUNT_ID")),
}
}
func (Book) Edges() []schema.Edge{
return []ent.Edge{
edge.To("account", Account.Type).
Unique().
Required().
Field("account_id").
Annotations(entgql.OrderField("ACCOUNT_NAME"))
}
}
```
gqlgen.yml:
```yml
schema:
- ent.graphql
resolver:
layout: follow-schema
dir: .
autobind:
- app/ent
models:
ID:
model:
- github.com/99designs/gqlgen/graphql.IntID
Node:
model:
- book/ent.Noder
```
GraphQL query:
```graphql
query {
books(first:10, orderBy: {direction: DESC, field: ACCOUNT_NAME}) {
edges {
node{
name
price
account{
name
}
}
cursor
}
}
}
```
Generated SQL query:
```sql
SELECT "public"."books"."id", "public"."books"."name", "public"."books"."price", "t1"."account_name"
FROM "public"."books"
LEFT JOIN (
SELECT "accounts"."id", "accounts"."name" AS "account_name"
FROM "accounts"
) AS "t1" ON "public"."books"."account_id" = "t1"."id"
ORDER BY "t1"."account_name" DESC NULLS LAST, "public"."books"."id" DESC
LIMIT 11
```
## Your Environment 🌎
| Tech | Version |
| ----------- | ------- |
| Go | 1.23.2 |
| Ent | 0.14.1 |
| Database | PostgreSQL |
| Driver | https://github.com/lib/pq |
Contributor guide
Assessment
This issue has not been assessed yet.