How best to use the new PostgreSQL 10 identity columns with Ecto?

Since PostgreSQL 10, there’s a new syntax for auto-incrementing IDs (id int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, see this blog post for more details).

I found a hacky solution for making Ecto use this new syntax by setting the following in myapp/config/config.exs:

config :myapp, MyApp.Repo,
  migration_primary_key: [name: :id, type: :"int GENERATED BY DEFAULT AS IDENTITY"]

However, injecting the default value into the type name is not really a nice way to do things.

I’ve tried setting it as

config :myapp, MyApp.Repo,
  migration_primary_key: [name: :id, type: :int, default: "GENERATED BY DEFAULT AS IDENTITY"]

But that causes an error: (Postgrex.Error) ERROR 22P02 (invalid_text_representation) invalid input syntax for integer: "GENERATED BY DEFAULT AS IDENTITY"

Any ideas for a neater way to do this?

6 Likes