 
   def get(flag_name) do
     name_string = to_string(flag_name)
-    
-    query = from r in Record, where: r.flag_name == ^name_string
-    results = repo().all(query)
-    flag = deserialize(flag_name, results)
-
-    {:ok, flag}
+    query = from(r in Record, where: r.flag_name == ^name_string)
+    try do
+      results = repo().all(query)
+      flag = deserialize(flag_name, results)
+      {:ok, flag}
+    rescue
+      e in [Ecto.QueryError] -> {:error, e}
+    end
   end
 
 
   def put(flag_name, gate = %Gate{}) do
     changeset = Record.build(flag_name, gate)
+    options = [
+      on_conflict: [set: [enabled: gate.enabled]],
+      conflict_target: [:flag_name, :gate_type, :target] # the unique index
+    ]
 
-    case repo().insert(changeset, on_conflict: [set: [enabled: gate.enabled]], conflict_target: [:flag_name, :gate_type, :target]) do
+    case repo().insert(changeset, options) do
       {:ok, _struct} ->
         {:ok, flag} = get(flag_name)
         publish_change(flag_name)

defmodule FunWithFlags.Store.Persistent.Ecto do
  @moduledoc false

  alias FunWithFlags.{Config, Gate, Flag}
  alias FunWithFlags.Store.Persistent.Ecto.Record
  alias FunWithFlags.Store.Serializer.Ecto, as: Serializer


      where: r.flag_name == ^flag_name
    )

    try do
      {_count, _} = repo().delete_all(query)
      {:ok, flag} = get(flag_name)
      publish_change(flag_name)
      {:ok, flag}
    rescue
      e in [Ecto.QueryError] -> {:error, e}
    end
  end


