Thanks to visit codestin.com
Credit goes to github.com

Skip to content

proposal: enable setting a DefaultFunc on a numeric type #1146

@thehowl

Description

@thehowl

I'm currently migrating a database from an existing structure where we have used a variation of Snowflake to generate the IDs for our resources. The natural way to store such a value in the database would be an uint64/BIGINT UNSIGNED NOT NULL, thus a numeric type for ent.

In spite of there not being a straight DefaultFunc method on the uint64Builder, by getting the Descriptor I managed to set a custom function which is ran when the resource is created:

	fd := field.Uint64("id").Immutable().Positive()
	fd.Descriptor().Default = func() uint64 {
		// generate...
	}

However, once we try to run entc, the following error shows up:

2021/01/07 10:08:32 running ent codegen:unmarshal schema [... json data ...]: unexpected default value type for field: "id"

Tracing back the error, it is from this method:

https://github.com/facebook/ent/blob/d4e04cd08f76389f0db76adec1c62d3a48c36d7b/entc/load/schema.go#L354-L369

Being a numeric type and having a default, the type conversion is attempted, but f.DefaultValue will actually be of type nil because only DefaultFunc is set.

Proposals to solve this:

  1. add a DefaultFunc check on the defaults() method, thus skipping it when we have such a function
  2. or, the former + add full support by also adding DefaultFunc on all numeric type builders - although this entails expanding the API

I'd be happy to work on a PR for this - but wanted to check first what's the best path to follow here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions