volatiletech / sqlboiler

Generate a Go ORM tailored to your database schema.
BSD 3-Clause "New" or "Revised" License
6.73k stars 544 forks source link

JSONB column generated as string field #300

Closed tcr-ableton closed 6 years ago

tcr-ableton commented 6 years ago

If you're having a generation problem please answer these questions before submitting your issue. Thanks!

What version of SQLBoiler are you using (sqlboiler --version)?

SQLBoiler v2.7.0

If this happened at generation time what was the full SQLBoiler command you used to generate your models? (if not applicable leave blank)

sqlboiler -b goose_migrations postgres

If this happened at runtime what code produced the issue? (if not applicable leave blank)

Here's the generated go code

// Version is an object representing the database table.
type Version struct {
    ID   int    `boil:"id" json:"id" toml:"id" yaml:"id"`
    Data string `boil:"data" json:"data" toml:"data" yaml:"data"`
    Sha  string `boil:"sha" json:"sha" toml:"sha" yaml:"sha"`

    R *versionR `boil:"-" json:"-" toml:"-" yaml:"-"`
    L versionL  `boil:"-" json:"-" toml:"-" yaml:"-"`
}

What is the output of the command above with the -d flag added to it? (Provided you are comfortable sharing this, it contains a blueprint of your schema)

{
        "Columns": [
            {
                "ArrType": null,
                "AutoGenerated": false,
                "DBType": "integer",
                "Default": "nextval('versions_id_seq'::regclass)",
                "FullDBType": "",
                "Name": "id",
                "Nullable": false,
                "Type": "int",
                "UDTName": "int4",
                "Unique": true,
                "Validated": false
            },
            {
                "ArrType": null,
                "AutoGenerated": false,
                "DBType": "jsonb",
                "Default": "",
                "FullDBType": "",
                "Name": "data",
                "Nullable": false,
                "Type": "types.JSON",
                "UDTName": "jsonb",
                "Unique": false,
                "Validated": false
            },
            {
                "ArrType": null,
                "AutoGenerated": false,
                "DBType": "character varying",
                "Default": "",
                "FullDBType": "",
                "Name": "sha",
                "Nullable": false,
                "Type": "string",
                "UDTName": "varchar",
                "Unique": true,
                "Validated": false
            },
            {
                "ArrType": null,
                "AutoGenerated": false,
                "DBType": "time with time zone",
                "Default": "",
                "FullDBType": "",
                "Name": "created_at",
                "Nullable": false,
                "Type": "time.Time",
                "UDTName": "timetz",
                "Unique": false,
                "Validated": false
            },
            {
                "ArrType": null,
                "AutoGenerated": false,
                "DBType": "time with time zone",
                "Default": "",
                "FullDBType": "",
                "Name": "updated_at",
                "Nullable": false,
                "Type": "time.Time",
                "UDTName": "timetz",
                "Unique": false,
                "Validated": false
            }
        ],
        "FKeys": null,
        "IsJoinTable": false,
        "Name": "versions",
        "PKey": {
            "Columns": [
                "id"
            ],
            "Name": "versions_pkey"
        },
        "SchemaName": "",
        "ToManyRelationships": [
            {
                "Column": "id",
                "ForeignColumn": "id",
                "ForeignColumnNullable": false,
                "ForeignColumnUnique": true,
                "ForeignTable": "songs",
                "JoinForeignColumn": "song_id",
                "JoinForeignColumnNullable": false,
                "JoinForeignColumnUnique": false,
                "JoinLocalColumn": "version_id",
                "JoinLocalColumnNullable": false,
                "JoinLocalColumnUnique": false,
                "JoinTable": "song_versions",
                "Nullable": false,
                "Table": "versions",
                "ToJoinTable": true,
                "Unique": true
            }
        ],
        "ToOneRelationships": null
    }

Please provide a relevant database schema so we can replicate your issue (Provided you are comfortable sharing this)

CREATE TABLE versions (
    id          serial      PRIMARY KEY,
    data        jsonb       NOT NULL,
    sha         varchar(64) UNIQUE NOT NULL,
    created_at  timetz      NOT NULL,
    updated_at  timetz      NOT NULL
);

CREATE UNIQUE INDEX versions_id_idx ON versions (id);
CREATE UNIQUE INDEX versions_sha_idx ON versions (sha);

Further information. What did you do, what did you expect?

Coming from gorm where I handled JSONB fields using the postgres.Jsonb type I am a bit confused as to what to expect as a proper generated output for jsonb fields. Certainly not what it generates now: a string.

I have found an older issue which seems to point to a dedicated JSON type in sqlboiler, but I can't get sqlboiler to generate it.

It feels like I missing a step or a piece of information and would need some help/examples regarding how to use and handle jsonb columns with sqlboiler. Any pointers?

Thanks! :-)

tcr-ableton commented 6 years ago

After tinkering around, destroying the models, and trying something else, multiple times, it seems fixed now. So I'll assume it was a problem with my dev environment rather than sqlboiler until proven otherwise.

I'll reopen if relevant :-)

Cheers