enrico

enrico

Error on updating a 'time' field to NULL using Ecto with Tds adapter

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!

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41989 114
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement