Passing database table names

defmodule WipeAndUpdatdeDB do
  alias Database.{Repo}

def main(table_name, list_of_updated_records) do
    Repo.transaction(fn ->
      Repo.delete_all(table_name)
      Print.text("Updating #{table_name} database table . . .")
      Repo.insert_all(table_name, list_of_updated_records)
      Print.highlight("#{table_name} database table update completed.")
    end)
  end

def test(records) do
	table_name = Database.SomeTable

	main(table_name, records)
end
end

This attempt to pass table_name to main/2 fails. How can table names be passed to main/2? (edited)

Can you copy/paste the error you’re receiving?

2021-06-02T18:11:37.462602+00:00 error: crasher: initial call: 'Elixir.Server.Monitor':init/1, pid: <0.368.0>, registered_name: 'Elixir.Server.Monitor', error: {#{'__exception__' => true,'__struct__' => 'Elixir.Postgrex.Error',connection_id => 3392990,message => nil,postgres => #{code => serialization_failure,file => <<"nodeModifyTable.c">>,line => <<"999">>,message => <<"could not serialize access due to concurrent delete">>,pg_code => <<"40001">>,routine => <<"ExecDelete">>,severity => <<"ERROR">>,unknown => <<"ERROR">>},query => nil},[{'Elixir.Ecto.Adapters.SQL',raise_sql_call_error,1,[{file,"lib/ecto/adapters/sql.ex"},{line,593}]},{'Elixir.Ecto.Adapters.SQL',execute,5,[{file,"lib/ecto/adapters/sql.ex"},{line,526}]},{'Elixir.WipeAndUpdatdeDB','-main/2-fun-0-',2,[{file,"lib/Helper Modules/WipeAndUpdateDB.ex"},{line,7}]},{'Elixir.Ecto.Adapters.SQL','-checkout_or_transaction/4-fun-0-',3,[{file,"lib/ecto/adapters/sql.ex"},{line,875}]},{'Elixir.DBConnection',run_transaction,4,[{file,"lib/db_connection.ex"},{line,1512}]},{'Elixir.Server.Monitor',handle_info,2,[{file,"lib/GenServers/Server.Monitor.ex"},{line,21}]},{gen_server,try_dispatch,4,[{file,"gen_server.erl"},{line,695}]},{gen_server,handle_msg,6,[{file,"gen_server.erl"},{line,771}]}]}, ancestors: [<0.352.0>,<0.349.0>,<0.79.0>], message_queue_len: 0, messages: [], links: [#Port<0.21>,<0.352.0>], dictionary: [], trap_exit: false, status: running, heap_size: 10958, stack_size: 29, reductions: 103578; neighbours:

Can you give an example of the kinds of record arguments you are supplying?

The message suggests Ecto is sending the table name correctly - this error indicates that one transaction is trying to delete (based on the mention of ExecDelete) a row that has already been deleted in another transaction (based on the code and message).

1 Like