Hi,
I am trying to set the value of a ‘time’ column to NULL using Ecto from a Phoenix app that interacts with a mssql database.
The problem comes when trying to update the value of a ‘time’ field to null (Inserts a struct is ok, even with null value).
I think it is not related to the original project because with a simple ‘ecto’ project the problem is the same: in the test project changing the database adapter to Postgres the problem disappears.
The test project is simple:
migration:
defmodule Hello.Repo.Migrations.CreateInfo do
use Ecto.Migration
def change do
create table(:infos) do
add :name, :string
add :alias, :string
add :simple_time, :time
timestamps()
end
create(unique_index(:infos, :alias))
end
end
schema:
defmodule Hello.Info do
use Ecto.Schema
import Ecto.Changeset
schema "infos" do
field(:alias, :string)
field(:name, :string)
field(:simple_time, :time)
timestamps()
end
def changeset(info, params \\ %{}) do
info
|> cast(params, [:name, :alias, :simple_time])
|> IO.inspect()
|> validate_required([:name, :alias])
|> unique_constraint(:alias)
end
end
Insertion is ok even with null value.
Update causes the error with this message from test suite:
defmodule Hello.InfoTest do
use Hello.RepoCase
...
test "update time field to nil" do
info = %Info{}
changeset = Info.changeset(info, @valid_attrs)
assert {:ok, info} = Repo.insert(changeset)
info = Repo.get!(Info, info.id)
{:ok, update_info} =
info
|> Info.changeset(%{simple_time: nil})
|> Repo.update()
assert update_info.simple_time == nil
end
...
> mix test
...
10:05:02.965 [debug] QUERY ERROR db=0.0ms queue=2.1ms
UPDATE [infos] SET [simple_time] = @1, [updated_at] = @2 WHERE [id] = @3 [nil, ~N[2021-05-27 08:05:02], 1]
1) test update time field to nil (Hello.InfoTest)
test/hello/info_test.exs:26
** (Tds.Error) Line 1 (Error 257): Implicit conversion from data type varbinary to time is not allowed. Use the CONVERT function to run this query.
code: |> Repo.update()
stacktrace:
(ecto_sql 3.6.1) lib/ecto/adapters/sql.ex:749: Ecto.Adapters.SQL.raise_sql_call_error/1
(ecto 3.6.1) lib/ecto/repo/schema.ex:717: Ecto.Repo.Schema.apply/4
(ecto 3.6.1) lib/ecto/repo/schema.ex:426: anonymous fn/15 in Ecto.Repo.Schema.do_update/4
test/hello/info_test.exs:36: (test)
...
any help is appreciated
Thanks in advance!