fireproofsocks

fireproofsocks

Storing IP address in Ecto

What’s best practice for storing IP addresses in Ecto? PostGres has network address fields — is there a way to take advantage of those? How should the migration and schema be defined? Thanks for any tips!

#ip

Most Liked

kip

kip

ex_cldr Core Team

Here is a more complete example, using EctoNetwork

Migration

Use the type :inet since thats a native Postgrex type

  def change do
    create table(:my_table) do
      ...
      add :net,             :inet
      timestamps()
    end
  end

Schema

Use the type EctoNetwork.INET

  schema "my_table" do
    ....
    field :net,             EctoNetwork.INET
    timestamps()
  end

Insert data

Option 1, insert using native data structures:

iex> Repo.insert %MyTable{net: %Postgrex.INET{address: {1,1,1,1}}}
07:01:58.042 [debug] QUERY OK db=7.8ms queue=1.1ms idle=1872.4ms
INSERT INTO "my_table" ("net","inserted_at","updated_at") VALUES ($1,$2,$3) [%Postgrex.INET{address: {1, 1, 1, 1}, netmask: 32}, ~N[2021-09-08 23:01:58], ~N[2021-09-08 23:01:58]]
{:ok,
 %MyTable{
   __meta__: #Ecto.Schema.Metadata<:loaded, "my_tables">,
   inserted_at: ~N[2021-09-08 23:01:58],
   net: %Postgrex.INET{address: {1, 1, 1, 1}, netmask: nil},
   updated_at: ~N[2021-09-08 23:01:58]
 }}

Option 2, preferred, by casting first (typically in a changeset):

iex> {:ok, addr} = EctoNetwork.INET.cast("1.1.1.1")
{:ok, %Postgrex.INET{address: {1, 1, 1, 1}, netmask: 32}}
iex> Repo.insert %MyTable{net: addr} 

07:11:19.288 [debug] QUERY OK db=1.3ms queue=0.9ms idle=1129.7ms
INSERT INTO "my_table" ("net","inserted_at","updated_at") VALUES ($1,$2,$3) [%Postgrex.INET{address: {1, 1, 1, 1}, netmask: 32}, ~N[2021-09-08 23:11:19], ~N[2021-09-08 23:11:19]]
{:ok,
 %Organization{
   __meta__: #Ecto.Schema.Metadata<:loaded, "my_table">,
   inserted_at: ~N[2021-09-08 23:11:19],
   net: %Postgrex.INET{address: {1, 1, 1, 1}, netmask: 32},
   updated_at: ~N[2021-09-08 23:11:19]
 }}
ruslandoga

ruslandoga

{127, 0, 0, 1} would need to be cast to EctoNetwork.INET before inserting. It can be done with changeset’s cast or manually with EctoNetwork.INET.cast which, in your case, would just turn the tuple into Postrex.INET struct.. That means if you only want to work with conn.remote_ip, using Postgrex.INET directly is an easy option as well.

kip

kip

ex_cldr Core Team

Postgrex which the Postgres driver supported by Ecto supports the inet data type so I suspect (but have not tested) that you can define a field of type :inet in your schema and you’re good to go.

LostKobrakai

LostKobrakai

In migrations you can basically make it create any possible column type postgres supports. You just need to find out how postgrex returns the data and maybe create a custom ecto type to work with it from elixir.

kip

kip

ex_cldr Core Team

In Option 1 you can omit using EctoNetwork. You would then change your schema definition to :map (instead of EctoNetwork.INET). And a quick test suggests that should work.

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement