tds icon indicating copy to clipboard operation
tds copied to clipboard

Random failure of character string to uniqueidentifier

Open jaybarra opened this issue 10 months ago • 0 comments

I will randomly get this error when doing batch inserts. I can have the same data being inserted over and over and it happens randomly.

** (Tds.Error) Line 1 (Error 8169): Conversion failed when converting from a character string to uniqueidentifier.
    (ecto_sql 3.12.1) lib/ecto/adapters/sql.ex:1096: Ecto.Adapters.SQL.raise_sql_call_error/1
    (ecto_sql 3.12.1) lib/ecto/adapters/sql.ex:967: Ecto.Adapters.SQL.insert_all/9
    (ecto 3.12.5) lib/ecto/repo/schema.ex:59: Ecto.Repo.Schema.do_insert_all/7
    iex:19: (file)
    iex:19: (file)
def find_the_poision() do
  bulk_inserts = [... ~100 static records]

  1..1000 |> Enum.each(fn _ -> 
    Repo.insert_all(MyRecord, bulk_inserts) 
  end)
end
defmodule MyRecord do
  use Ecto.Schema

  @primary_key {:id, :binary_id, autogenerate: true, source: :Id}
  @foreign_key_type :binary_id

  schema "MyRecords" do
    field :title, :string, source: :Title

    belongs_to :parent, Parent, source: :ParentId
  end
end

defmodule Parent do
  use Ecto.Schema

  @primary_key {:id, :binary_id, autogenerate: true, source: :Id}
  @foreign_key_type :binary_id

  schema "Parents" do
    has_many :records, MyRecord
  end
end

Environment and library versions

SQL Server 16 phoenix_ecto 4.6 ecto_sql 3.12 tds 2.3

jaybarra avatar Apr 03 '25 14:04 jaybarra