Skip to content

Commit 96415e3

Browse files
Correctly handle generated array columns (#661)
1 parent 486a3ad commit 96415e3

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

lib/ecto/adapters/postgres/connection.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,8 +1750,10 @@ if Code.ensure_loaded?(Postgrex) do
17501750
defp options_expr(options),
17511751
do: [?\s, options]
17521752

1753-
defp column_type({:array, type}, opts),
1754-
do: [column_type(type, opts), "[]"]
1753+
defp column_type({:array, type}, opts) do
1754+
[type, opts] = column_type(type, opts)
1755+
[type, "[]", opts]
1756+
end
17551757

17561758
defp column_type(type, opts) when type in ~w(time utc_datetime naive_datetime)a do
17571759
generated = Keyword.get(opts, :generated)

test/ecto/adapters/postgres_test.exs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2331,12 +2331,13 @@ 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|]}
2334+
{:add, :id_str, :string, [generated: ~s|ALWAYS AS (id) STORED|]},
2335+
{:add, :tags, {:array, :text}, [generated: ~s|ALWAYS AS (ARRAY['foo','bar']) STORED|]}
23352336
]}
23362337

23372338
assert execute_ddl(create) == [
23382339
"""
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+
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"))
23402341
"""
23412342
|> remove_newlines
23422343
]

0 commit comments

Comments
 (0)