postgresqlectoelixirpostgrex

Error trying to implement "geo" postgrex extention type


I'm following this tutorial which adds a postgrex extension into the config.exs with the extensions field. However that is now a deprecated way to add a postgrex extension, we should now use the type field instead of the extensions field. I'm following the code on the geo library github page, to add the extension:

config.exs

Postgrex.Types.define(MyApp.PostgresTypes,
  [Geo.PostGIS.Extension] ++ Ecto.Adapters.Postgres.extensions(),
  json: Poison)

use Mix.Config

config :api, Api.Repo,
  types: MyApp.PostgresTypes,
  adapter: Ecto.Adapters.Postgres,
  database: "api_repo",
  username: "postgres",
  password: "postgres",
  hostname: "localhost",
  web_port: String.to_integer(System.get_env("PORT") || "4000"),
  timeout: 60_000,
  pool_timeout: 60_000

config :api, ecto_repos: [Api.Repo]

But I'm getting this error:

bash-3.2$ mix ecto.migrate
** (Mix.Config.LoadError) could not load config config/config.exs
    ** (UndefinedFunctionError) function Ecto.Adapters.Postgres.extensions/0 is undefined (module Ecto.Adapt
ers.Postgres is not available)
    Ecto.Adapters.Postgres.extensions()
    (stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
    (stdlib) erl_eval.erl:470: :erl_eval.expr/5
    (stdlib) erl_eval.erl:878: :erl_eval.expr_list/6
    (stdlib) erl_eval.erl:404: :erl_eval.expr/5
    (stdlib) erl_eval.erl:122: :erl_eval.exprs/5

I have seen online this is sometimes due to a version of postgrex however that is a very old version and I'm using {:postgrex, "~> 0.13.2"},. What am I missing?


Solution

  • The line defining the custom Postgrex types needs to go in a .ex file that is compiled with your app (anything in lib/ and web/), not in a config file. Configuration files do not have access to the application's dependencies' functions.

    So, if you move this code to e.g. lib/my_app/postgres_types.ex:

    Postgrex.Types.define(MyApp.PostgresTypes,
      [Geo.PostGIS.Extension] ++ Ecto.Adapters.Postgres.extensions(),
      json: Poison)
    

    Your shouldn't get the undefined function error anymore and everything should work assuming you've followed the rest of the instructions in the geo package correctly.