go-jet / jet

Type safe SQL builder with code generation and automatic query result data mapping
Apache License 2.0
2.49k stars 117 forks source link

Cross Schema References #388

Open safaci2000 opened 1 week ago

safaci2000 commented 1 week ago

Describe the bug If you have a datatype defined in Schema A and need to reference it in Schema B, Jet code gen seems to be unable to determine where the type is defined.

**Environment (please complete the following information):**
 - OS: macosx
 - Database: postgres
-  Database driver: pq
 - Jet version 2.11.1

**Code snippet**

For example:

```sql
CREATE SCHEMA storage;
CREATE TYPE storage.environment AS ENUM (
    'local',
    'staging',
    'prod'
);

CREATE TABLE funcap.controller_status (
    environment_name storage.environment,
    ready boolean
);

The generated code is then confused.

type ControllerStatus struct {
    EnvironmentName *Environment `json:"EnvironmentName" db:"controller_status.environment_name" alias:"controller_status.environment_name"`
    Ready           *bool        `json:"Ready" db:"controller_status.ready" alias:"controller_status.ready"`
}

since the type Environment doesn't exist within the package it needs to have a namespace reference.  

Expected behavior I would expect the generated code to compile

go-jet commented 5 days ago

The workaround for this issue is to first generate schema A, then customize the generator for schema B to use the enum model type from schema A.

safaci2000 commented 5 days ago

Okay, thanks. That's good to know. It's not my favorite, since I can't really wipe the generated output and re-create without some code editing, but I'll take it.

I still think at some point having a way to run the jet generator where it loads all known schemas would make Jet a lot more powerful, plus it would be aware of all the given data types.