Postgresql binary format field in Phoenix/Ecto

I’m trying to figure out how to translate an existing database field from a Rails model that has a binary format. It is used for storing uploaded file contents. I don’t see :binary as an option in https://hexdocs.pm/postgrex/readme.html#Data-representation. Would ‘bytea’ be a suitable alternative?

Thanks!

2 Likes

You could create a custom type in Ecto?

In a recent project, I had the chance to use JSON in Ecto. Although Ecto does not currently support JSON, it does provide us with the capability to define custom types. In this blog post, we’ll go through how we can use the JSON type in Ecto. Did I mention JSON enough already?

http://gabrieljaldon.com/articles/using-json-type-in-ecto.html

3 Likes

Thanks - I will give that path a try.

1 Like

to store uploaded files :bytea works for me:

   create table(:files) do
      add :data, :bytea
      timestamps
    end

 use App.Web :model
  schema "files" do
    field :data, :binary
    timestamps
  end
4 Likes

Looking through the PG docs, looks like Bytea is their binary format? http://www.postgresql.org/docs/9.0/static/datatype-binary.html

1 Like

Thanks, all!

1 Like