ksherman

ksherman

Tds.Ecto: custom primary key and OUTPUT/INSERT DML statement error

Yo! So I’m running into a little issue I’d like to get some advice on.

I have an existing MSSQL database I’m retrofitting Elixir/Phx/Ecto on top of. I have to have tons of custom @primary_keys on, many of these tables have post insert/update triggers, which is where the issue stems from. The error I’m running into is when I insert the record, I usually get this error message:

(Tds.Error) Line 1 (Error 334): The target table 'Users' of the DML statement cannot have any enabled triggers if the statement contains an OUTPUT clause without INTO clause.

The query usually looks like:

[debug] QUERY ERROR db=80.4ms
INSERT INTO [Users] ([EmailAddress], [FirstName], [LastName], [UserPassword]) OUTPUT INSERTED.[UserID] VALUES (@1, @2, @3, @4) ["kevin+000@kdsherm.com", "Kevin", "Sherman", "sup"]

This is similar to a few issues I’ve tracked down:
https://github.com/findmypast-oss/mssql_ecto/issues/27
https://github.com/livehelpnow/tds_ecto/issues/61

Now, I can get through the error message by doing something like @primary_key {:UserID, :integer, []} in my schemas, or Repo.insert(returning: false) this helps to remove the OUTPUT clause in the query and the record is saved in the database. Cool.

But the next issue is that okay, the record persists, but without the OUTPUT clause, I don’t get the generated UserID back from the query in the struct:

%EfAPI.Data.User{
  EmailAddress: "kevin+000@kdsherm.com",
  FirstName: "Kevin",
  LastName: "Sherman",
  UserID: nil,
  ZipCodeDesc1: nil,
  __meta__: #Ecto.Schema.Metadata<:loaded, "Users">,
}

So given that I’m in funky territory with an existing database with triggers (I knowwww, but not a lot I can do about triggers for a while), what’s a good strategy to get the record I just created?

My two thoughts: if I validate the uniqueness of the email, I could use the email address to pull up the record; second was I need to generate password hashes, those should be unique, maybe I could use the password hash in a query to pull the full record? Or am I missing a slick idea :tm:?

Bonus thought: I see an INTO clause in the query, so what’s the deal there?! :sob:

Thanks all!

First Post!

mjaric

mjaric

Hi @ksherman, there seems to be some improvements in tds library that could fixed this issue. Wouldn’t it be too much to ask if you can try your scenario with ecto_sql fork here

Where Next?

Popular in Questions Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement