Skip to content

Commit 2ee3db4

Browse files
newmanjeffJeff Newman
and
Jeff Newman
authored
Enable AUTOINCREMENT for serial and bigserial (#98)
Co-authored-by: Jeff Newman <[email protected]>
1 parent 707fe8e commit 2ee3db4

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

lib/ecto/adapters/sqlite3/connection.ex

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,20 +1581,12 @@ defmodule Ecto.Adapters.SQLite3.Connection do
15811581
collate = Keyword.get(opts, :collate)
15821582
check = Keyword.get(opts, :check)
15831583

1584-
column_options(default, type, null, pk, collate, check)
1585-
end
1586-
1587-
defp column_options(_default, :serial, _, true, _, _) do
1588-
" PRIMARY KEY AUTOINCREMENT"
1589-
end
1590-
1591-
defp column_options(default, type, null, pk, collate, check) do
15921584
[
15931585
default_expr(default, type),
15941586
null_expr(null),
15951587
collate_expr(collate),
15961588
check_expr(check),
1597-
pk_expr(pk)
1589+
pk_expr(pk, type)
15981590
]
15991591
end
16001592

@@ -1660,8 +1652,11 @@ defmodule Ecto.Adapters.SQLite3.Connection do
16601652
defp index_expr(literal) when is_binary(literal), do: literal
16611653
defp index_expr(literal), do: quote_name(literal)
16621654

1663-
defp pk_expr(true), do: " PRIMARY KEY"
1664-
defp pk_expr(_), do: []
1655+
defp pk_expr(true, type) when type in [:serial, :bigserial],
1656+
do: " PRIMARY KEY AUTOINCREMENT"
1657+
1658+
defp pk_expr(true, _), do: " PRIMARY KEY"
1659+
defp pk_expr(_, _), do: []
16651660

16661661
defp options_expr(nil), do: []
16671662

test/ecto/adapters/sqlite3/connection_test.exs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2787,4 +2787,22 @@ defmodule Ecto.Adapters.SQLite3.ConnectionTest do
27872787

27882788
assert query == ~s{SELECT p0."id", p0."title", p0."content" FROM "posts" AS p0}
27892789
end
2790+
2791+
test "autoincrement support" do
2792+
serial = {:create, table(:posts), [{:add, :id, :serial, [primary_key: true]}]}
2793+
bigserial = {:create, table(:posts), [{:add, :id, :bigserial, [primary_key: true]}]}
2794+
id = {:create, table(:posts), [{:add, :id, :id, [primary_key: true]}]}
2795+
integer = {:create, table(:posts), [{:add, :id, :integer, [primary_key: true]}]}
2796+
2797+
assert execute_ddl(serial) == [
2798+
~s/CREATE TABLE "posts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT)/
2799+
]
2800+
2801+
assert execute_ddl(bigserial) == [
2802+
~s/CREATE TABLE "posts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT)/
2803+
]
2804+
2805+
assert execute_ddl(id) == [~s/CREATE TABLE "posts" ("id" INTEGER PRIMARY KEY)/]
2806+
assert execute_ddl(integer) == [~s/CREATE TABLE "posts" ("id" INTEGER PRIMARY KEY)/]
2807+
end
27902808
end

0 commit comments

Comments
 (0)