Collection of Nested Forms Association ID Increments

I’ve been building a collection of nested forms which can be seen at NestedForm using Phoenix 1.4 and Elixir 1.7.4.

I’ve made it to the point of adding my second nested form but I’ve encountered something that might be beyond my understanding of cast_assoc/2.
I’ve got all the forms showing up in :new and it correctly writes the input data to the postgres database. When I only had a single nested form :siteinfo the store_id indentically matched the :stores's id. After adding a second nested form, including a second cast_assoc/2 in the changeset the store_id has been incremented by 1, causing my :show form to give me a query error:

# expected at least one result but got none in query:
from s in Nestedform.Stores.Siteinfo,
where: == ^"6"

You can also see this represented by this screenshot from PgAdmin where I added the second nested form beginning at id: 3 (edited to remove extra data columns)

Where do I tackle this issue? In the controller’s query like:

def show(conn, %{"id" => id}) do
  store = Stores.get_store!(id)
  siteinfo = Stores.get_siteinfo!(id+1)
  internetconnection = Stores.get_internet_connection!(id)
  render(conn, "show.html", store: store, siteinfo: siteinfo, internetconnection: 

or in the context like:

def get_siteinfo!(id), do: Repo.get!(Siteinfo, id+1)

my syntax is probably wrong for incrementing an integer like that and I think this solution isn’t ideal since I will be adding more forms to this collection and I don’t want to have to track how each id is getting incremented. Is there another way to go about it?


I think you are confusing the id’s of Store and Siteinfo. They can be the same or vary. But the important part is that they are associated with the store_id field in the Siteinfo table.

Try this to get the corresponding Siteinfo and InternetConnection of a Store:

store = Repo.get!(Store, 1) |> Repo.preload([:siteinfo, :internetconnection])

You can see them: store.siteinfo and store.internetconnection

I hope this helps,

Best regards,

1 Like