# Dynamically reference a module spec

**URL:** <https://forum.elixirforum.com/t/dynamically-reference-a-module-spec/61675>\
**Category:** Questions\
**Tags:** module, dynamic, spec\
**Created:** [February 15, 2024, 8:41am UTC](https://forum.elixirforum.com/t/dynamically-reference-a-module-spec/61675 "2024-02-15T08:41:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![erix](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/erix/32/33651_2.png) [@erix](https://forum.elixirforum.com/u/erix)\
**Post date:** [February 15, 2024, 8:41am UTC](https://forum.elixirforum.com/t/dynamically-reference-a-module-spec/61675/1 "2024-02-15T08:41:02Z")

</div>

I have a module for each model in my program, and I can query them dynamically:

```elixir
model = String.to_existing_atom( "Elixir." <> data.params.model )
query = from m in model,
  where: m.domain == ^data.payload.domain,
  select: %{
    :id => m.id,
    :date => m.date,
    :domain => m.domain
  }
result = Cc.MySQL.one(query)

```

where:

1. `data.params.model` is `CC.Mpesa.Woo.Model`, set earlier in the program from constants
2. `query` is using `import Ecto.Query, only: [from: 2]`

This works with no issue.

My question, now, is how to reference a module spec dynamically.

If I statically set the spec:

```elixir
with {:ok, add} <- Cc.MySQL.insert %CC.Mpesa.Woo.Model{
  date: today,
  domain: data.payload.domain,
  agent: agent,
  key: @tools.makeid,
} do

```

The data will be inserted.

However I get an error when I place model here instead:

```elixir
with {:ok, add} <- Cc.MySQL.insert %model{
  date: today,
  domain: data.payload.domain,
  agent: agent,
  key: @tools.makeid,
} do

```

Error:

```elixir
    error: expected struct name to be a compile time atom or alias, got: model
    │
 32 │ with {:ok, add} <- Cc.MySQL.insert %model{
    │ ^

```

How would I insert `model` as an atom here, so that the spec (schema) is used?

---

<div class="post-metadata">

**Author:** ![jswanner](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jswanner/32/24585_2.png) [@jswanner](https://forum.elixirforum.com/u/jswanner)\
**Post date:** [February 15, 2024, 8:45am UTC](https://forum.elixirforum.com/t/dynamically-reference-a-module-spec/61675/2 "2024-02-15T08:45:56Z")

</div>

`struct!(model, date: today, ...)`

---

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/32/38664_2.png) [@dimitarvp](https://forum.elixirforum.com/u/dimitarvp)\
**Post date:** [February 15, 2024, 6:36pm UTC](https://forum.elixirforum.com/t/dynamically-reference-a-module-spec/61675/3 "2024-02-15T18:36:14Z")

</div>

> [@erix](#):
>
> `model = String.to_existing_atom( "Elixir." <> data.params.model )`

Don’t use this. Use that instead:

```elixir
model = Module.concat([data.params.model])

```

Notice that the function argument is a list with a single item inside.
