Skip to content

Commit 37a7af6

Browse files
committed
fix: Better error message for subscribing to inserts by primary key
1 parent 23cd52c commit 37a7af6

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/ecto_watch/watcher_server.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ defmodule EctoWatch.WatcherServer do
184184
defp validate_subscription(state, identifier, column) do
185185
cond do
186186
match?({_, :inserted}, identifier) && column == state.options.schema_definition.primary_key ->
187-
{:error, "Cannot subscribe to primary_key for inserted records"}
187+
{:error,
188+
"Cannot subscribe to primary_key for inserted records because primary key values aren't created until the insert happens"}
188189

189190
column && not MapSet.member?(state.identifier_columns, column) ->
190191
{:error, "Column #{column} is not an association column"}

test/ecto_watch_test.exs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,23 @@ defmodule EctoWatchTest do
819819
end
820820

821821
describe "inserts" do
822+
test "Error when trying to subscribe to insert of an ID" do
823+
start_supervised!(
824+
{EctoWatch,
825+
repo: TestRepo,
826+
pub_sub: TestPubSub,
827+
watchers: [
828+
{Thing, :inserted}
829+
]}
830+
)
831+
832+
assert_raise ArgumentError,
833+
~r/Cannot subscribe to primary_key for inserted records because primary key values aren't created until the insert happens/,
834+
fn ->
835+
EctoWatch.subscribe({Thing, :inserted}, 123)
836+
end
837+
end
838+
822839
test "get notification about inserts" do
823840
start_supervised!({EctoWatch,
824841
repo: TestRepo,

0 commit comments

Comments
 (0)