knvjun

knvjun

Ecto - Multidimensional Array?

Hi Elixir community!

I’m trying to add a column with a two dimensional array type to database.
Here’s my migration.

def change do
    alter table(:decisions) do
      add :sequence, {:array, {:array, :string}}
    end
end

with this, i don’t get error running migration.

Here’s my schema

  schema "decisions" do
    ...
    field :sequence, {:array, {:array, :string}}

    timestamps()
  end

But when i try to save this value [["C83", "N51", "G3"], ["K22", "B17"], ["W46", "N14"]] to field :sequence, I got this error

(ArgumentError) nested lists must have lists with matching lengths
        (postgrex) lib/postgrex/extensions/array.ex:63: anonymous fn/6 in Postgrex.Extensions.Array.encode/4

How do I put matching lists with matching lengths to :sequence column? Any help is appreciated.

Thank You.

Marked As Solved

NobbZ

NobbZ

All of the inner lists, need to have the same length. Zour first list has three elements, the remaining ones have only 2.

Either make all lists of length 3 or all of them of length 2.

Also Liked

alco

alco

I’m not so sure about that:

CREATE TEMPORARY TABLE sal_emp (
    name            text,
    pay_by_quarter  integer[],
    schedule        text[][]
);
# CREATE TABLE

INSERT INTO sal_emp
    VALUES ('Bill',
    '{10000, 10000, 10000, 10000}',
    '{{"meeting", "lunch"}, {"meeting"}}');
# ERROR:  malformed array literal: "{{"meeting", "lunch"}, {"meeting"}}"
# LINE 4:     '{{"meeting", "lunch"}, {"meeting"}}');
#             ^
# DETAIL:  Multidimensional arrays must have sub-arrays with matching dimensions.

INSERT INTO sal_emp
    VALUES ('Bill',
    '{10000, 10000, 10000, 10000}',
    '{{"meeting", "lunch"}, {"lunch", "meeting"}}');
# INSERT 0 1
Phillipp

Phillipp

Out of curiosity, what is the reason for that requirement?

mudasobwa

mudasobwa

Creator of Cure

I am also interested in this. AFAIK, PostgreSQL is fine with arbitrary length lists.

mudasobwa

mudasobwa

Creator of Cure

Indeed. I have re-checked myself; it turns out the lengths of sub-arrays are to be the same. Thanks.

Where Next?

Popular in Questions Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
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
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement