Ent: proposal: add support for user defined types

Created on 26 Jun 2020  路  5Comments  路  Source: ent/ent

Given a schema like the one below and a database that has had CREATE EXTENSION postgis run on it, the auto-migration works perfectly fine. The first time. Running it again on the same database with no changes to the schema results in sql/schema: invalid type "USER-DEFINED" for column "geo".

package schema

import (
    "github.com/facebookincubator/ent"
    "github.com/facebookincubator/ent/dialect"
    "github.com/facebookincubator/ent/schema/field"
)

// Geography holds the schema definition for the Geography entity.
type Geography struct {
    ent.Schema
}

// Fields of the Geography.
func (Geography) Fields() []ent.Field {
    return []ent.Field{
        field.Bytes("geo").
            SchemaType(map[string]string{
                dialect.Postgres: "geography",
            }),
    }
}

Edit: Fixed accidental usage of field.String() instead of field.Bytes(). Doesn't really make a difference for this bug, but this way the type matches what the field actually scans as.

Proposal

All 5 comments

I have the same issue. We added a wrapper to the migrate to run CREATE EXTENSION ltree before create but it gives us sql/schema: invalid type "USER-DEFINED" for column "path" from time to time. The creation of the schema looks like this:

type Team struct {
    ent.Schema
}

// Fields of the Team.
func (Team) Fields() []ent.Field {
    return []ent.Field{
        field.String("name").NotEmpty(),
        // path of the team from the root node.
        // Path needs to follow the ltree format <ident>.<ident>.<ident>
        field.String("path").SchemaType(map[string]string{
            dialect.Postgres: "ltree",
        }),
    }
}

@a8m Do you think this is a problem in my code? Is there anywhere else I can create an extension?

Hey @DeedleFake and @christie-zhao,
The schema migration doesn't work with "user defined types" at the moment - it's WIP and will be added in the near future.

@a8m Do you have any pointers as to why it doesn't work / what could be done to implement it? I'd be happy to implement it, just not quite sure where to begin 馃槄

This has been partially implemented by #994

Migrations will work so long as the custom types don't change. currently the is covertible logic will disallow conversion between user defined types.

This could be agumented by adding a a check if a dialect specific schema type is present but I am not sure how useful it will be

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jbub picture jbub  路  5Comments

clearcodecn picture clearcodecn  路  3Comments

rotemtam picture rotemtam  路  4Comments

sno6 picture sno6  路  4Comments

BitPhinix picture BitPhinix  路  5Comments