diff --git a/lib/ecto/adapters/postgres/connection.ex b/lib/ecto/adapters/postgres/connection.ex index 746257ed..0f33ab89 100644 --- a/lib/ecto/adapters/postgres/connection.ex +++ b/lib/ecto/adapters/postgres/connection.ex @@ -1762,7 +1762,7 @@ if Code.ensure_loaded?(Postgrex) do defp column_type({:array, type}, opts) do [type, opts] = column_type(type, opts) - [type, "[]", opts] + [[type, "[]"], opts] end defp column_type(type, opts) when type in ~w(time utc_datetime naive_datetime)a do diff --git a/test/ecto/adapters/postgres_test.exs b/test/ecto/adapters/postgres_test.exs index b88d35fc..6f59ac58 100644 --- a/test/ecto/adapters/postgres_test.exs +++ b/test/ecto/adapters/postgres_test.exs @@ -2331,13 +2331,41 @@ defmodule Ecto.Adapters.PostgresTest do primary_key: true, generated: "ALWAYS AS IDENTITY (MINVALUE 0 START WITH 0 INCREMENT BY 1)" ]}, - {:add, :id_str, :string, [generated: ~s|ALWAYS AS (id) STORED|]}, - {:add, :tags, {:array, :text}, [generated: ~s|ALWAYS AS (ARRAY['foo','bar']) STORED|]} + {:add, :id_str, :string, [generated: ~s|ALWAYS AS (id) STORED|]} ]} assert execute_ddl(create) == [ """ - 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")) + 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")) + """ + |> remove_newlines + ] + end + + test "create table with generated array column" do + create = + {:create, table(:posts), + [{:add, :tags, {:array, :text}, [generated: ~s|ALWAYS AS (ARRAY['foo','bar']) STORED|]}]} + + assert execute_ddl(create) == [ + """ + CREATE TABLE "posts" ("tags" text[] GENERATED ALWAYS AS (ARRAY['foo','bar']) STORED) + """ + |> remove_newlines + ] + end + + test "create table with generated nested array column" do + create = + {:create, table(:posts), + [ + {:add, :tags, {:array, {:array, :text}}, + [generated: ~s|ALWAYS AS (ARRAY['foo','bar']) STORED|]} + ]} + + assert execute_ddl(create) == [ + """ + CREATE TABLE "posts" ("tags" text[][] GENERATED ALWAYS AS (ARRAY['foo','bar']) STORED) """ |> remove_newlines ]