ent icon indicating copy to clipboard operation
ent copied to clipboard

feat: Postgres Native Enums

Open cliedeman opened this issue 5 years ago • 5 comments

Resolves https://github.com/facebook/ent/issues/872

func (Person) Fields() []ent.Field {
	return []ent.Field{
		field.Int("id"),
		field.Enum("mood").GoType(mood.Mood("mood")).SchemaType(map[string]string{dialect.Postgres: "mood"}),
	}
}

Notes

  • alter type is not used because it cannot run in a transaction and cannot remove types
  • the current implementation will drop enum types that ent does not know about
  • migration to and from enums are supported but this assumes that in the varchar -> enum case all the values are valid enum values

cliedeman avatar Oct 28 '20 07:10 cliedeman

Not sure why generate is failing

cliedeman avatar Oct 28 '20 07:10 cliedeman

Hey @cliedeman and thanks for adding this! It's really amazing.

I'll start giving it a look tomorrow, but I can say now, that I prefer to merge it in small chunks (split it into multiple PRs) - I can help you with that tomorrow.

BTW, it is ready for review, or you still work on it?

a8m avatar Oct 28 '20 20:10 a8m

Hi @a8m It is ready for review. I only expect to add code. More unit tests and some validation of the enums (E.g. a user should not be allowed to specify the same enum on 2 different tables with differing values).

Happy to split it into chunks as you see fit

cliedeman avatar Oct 29 '20 04:10 cliedeman

@a8m I have converted this pr to draft while I split this out into smaller pr's.

I should get to it in the next few days

cliedeman avatar Nov 25 '20 08:11 cliedeman

Hi @cliedeman was there any update in the meantime on this topic?

mvrahden avatar Sep 04 '21 16:09 mvrahden

Really need this feature, worked around it by writing custom migrations just for enums, but now Atlas complains with the below error ariga.io/atlas/sql/postgres.(*state).mayDropEnum because I guess it does not know how to drop the enum

juicycleff avatar Feb 19 '23 09:02 juicycleff

Really need this feature, worked around it by writing custom migrations just for enums, but now Atlas complains with the below error ariga.io/atlas/sql/postgres.(*state).mayDropEnum because I guess it does not know how to drop the enum

Try out https://github.com/mvrahden/go-enumer to generate enums. It has a compatibility option with ent

mvrahden avatar Feb 19 '23 17:02 mvrahden