2
votes

I am trying to add geospatial fields to the phoenix framework with https://github.com/bryanjos/geo package. I want to access through Ecto models so I followed the outlined of adding the line:

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

as outlined in the readme file. Problem is that it doesn't specify exactly where to add this so I added it to config/dev.exs

I then added the line:

types: MyApp.PostgresTypes

to my dev repo config file config/dev.exs as specified.

I then get the error that Postgrex.Types.define and Ecto.Adapters.Postgres.extensions functions do not exist (as the modules are reportedly unavailable.

I then try to put the following lines in lib/myapp/myapp.ex as I suspect it has something to do with process intialization.

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

However when I add more fields do another migrate with mix ecto.migrate, I get an error:

function Jobflo.PostgresTypes.find/2 is undefined (module Jobflo.PostgresTypes is not available.

I don't think it should be this hard and suspect I am making an obvious mistake. Anyone familiar with setting up Geo stuff with Phoenix would be greatly appreciated. Thankyou.

1
Post relevant code here; don't link your post to github, please.BusyProgrammer

1 Answers

4
votes

I found this a tad confusing when I upgraded geo a few months ago.

Create a file called lib/postgres_types.ex with the following in it:

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

In your config file (in my case, "#{Mix.env}.exs") make sure your config looks like this:

config :myapp, Myapp.Repo,
  adapter: Ecto.Adapters.Postgres,
  types:   Myapp.PostgresTypes,
  username: "jbloggs",
  password: "abc123",
  database: "api",
  hostname: "db_hostname.com",
  pool_size: 10

I hope this helps you!