Skip to content

Commit 35e2798

Browse files
Fix nested array generated columns (#676)
1 parent 955f0fb commit 35e2798

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

lib/ecto/adapters/postgres/connection.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1762,7 +1762,7 @@ if Code.ensure_loaded?(Postgrex) do
17621762

17631763
defp column_type({:array, type}, opts) do
17641764
[type, opts] = column_type(type, opts)
1765-
[type, "[]", opts]
1765+
[[type, "[]"], opts]
17661766
end
17671767

17681768
defp column_type(type, opts) when type in ~w(time utc_datetime naive_datetime)a do

test/ecto/adapters/postgres_test.exs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2331,13 +2331,41 @@ defmodule Ecto.Adapters.PostgresTest do
23312331
primary_key: true,
23322332
generated: "ALWAYS AS IDENTITY (MINVALUE 0 START WITH 0 INCREMENT BY 1)"
23332333
]},
2334-
{:add, :id_str, :string, [generated: ~s|ALWAYS AS (id) STORED|]},
2335-
{:add, :tags, {:array, :text}, [generated: ~s|ALWAYS AS (ARRAY['foo','bar']) STORED|]}
2334+
{:add, :id_str, :string, [generated: ~s|ALWAYS AS (id) STORED|]}
23362335
]}
23372336

23382337
assert execute_ddl(create) == [
23392338
"""
2340-
CREATE TABLE "posts" ("id" integer GENERATED ALWAYS AS IDENTITY (MINVALUE 0 START WITH 0 INCREMENT BY 1), "id_str" varchar(255) GENERATED ALWAYS AS (id) STORED, \"tags\" text[] GENERATED ALWAYS AS (ARRAY['foo','bar']) STORED, PRIMARY KEY ("id"))
2339+
CREATE TABLE "posts" ("id" integer GENERATED ALWAYS AS IDENTITY (MINVALUE 0 START WITH 0 INCREMENT BY 1), "id_str" varchar(255) GENERATED ALWAYS AS (id) STORED, PRIMARY KEY ("id"))
2340+
"""
2341+
|> remove_newlines
2342+
]
2343+
end
2344+
2345+
test "create table with generated array column" do
2346+
create =
2347+
{:create, table(:posts),
2348+
[{:add, :tags, {:array, :text}, [generated: ~s|ALWAYS AS (ARRAY['foo','bar']) STORED|]}]}
2349+
2350+
assert execute_ddl(create) == [
2351+
"""
2352+
CREATE TABLE "posts" ("tags" text[] GENERATED ALWAYS AS (ARRAY['foo','bar']) STORED)
2353+
"""
2354+
|> remove_newlines
2355+
]
2356+
end
2357+
2358+
test "create table with generated nested array column" do
2359+
create =
2360+
{:create, table(:posts),
2361+
[
2362+
{:add, :tags, {:array, {:array, :text}},
2363+
[generated: ~s|ALWAYS AS (ARRAY['foo','bar']) STORED|]}
2364+
]}
2365+
2366+
assert execute_ddl(create) == [
2367+
"""
2368+
CREATE TABLE "posts" ("tags" text[][] GENERATED ALWAYS AS (ARRAY['foo','bar']) STORED)
23412369
"""
23422370
|> remove_newlines
23432371
]

0 commit comments

Comments
 (0)