Creating a user through terminal

Thanks for stopping by and helping me with this one. I’m trying to test models I create before continuing with the project but continue to produce an error.

What I’ve done so far was run:

mix phx.gen.context Accounts User users username:string:unique

I’m trying to create a user but when I try

alias Account.User
User.changeset(%User{}, %{}) 

I’m getting an error of

** (CompileError) iex:2: Account.User.__struct__/1 is undefined, cannot expand struct Account.User
    (stdlib) lists.erl:1354: :lists.mapfoldl/3

Was hoping to get a better understanding of what I’ve been doing wrong when I’m trying to test in the terminal.

Thank you for the help and if my explanation was horrible. I’m sorry and please ask me questions.

Happy 4th of July!

So you generated a new phoenix project and followed this steps:

  • mix name_of_application or mix --no-html --no-webpack(api)

  • then setup the dev.exs database info

  • mix ecto.create

  • mix phx.gen.context Accounts User users username:string:unique

  • mix ecto.migrate

Also for getting started I recommend you to start with this

Shouldn’t that be alias Accounts.User (plural Accounts)?

Thank you for replying to my question.

That is correct. I’ve done each step for

  • setup the dev.exs database info

I was wondering why I was getting the error since I alias the User schema meaning we should be able to use the property like changeset and it’s schema as well. Doesn’t the schema play a similar role to a struct? where we would be able to use %User{ username: “something” } ?

Thanks for the link as well. I’ll be taking a look at it.

That is correct. Thank you for catching that mistake I made. Although once I tried it with Accounts, I still get the same error as

alias Accounts.User 
iex(4)> User.changeset(%User{}, %{})
** (CompileError) iex:4: Accounts.User.__struct__/1 is undefined, cannot expand struct Accounts.User
    (stdlib) lists.erl:1354: :lists.mapfoldl/3

The official docs is here

Also this is what they say about it

Changesets allow filtering, casting, validation and definition of constraints when manipulating structs.

Then please tell us what the exact module names of the generated context and struct are. Maybe they are namespaced by your application (this is very likely!).

1 Like

I think that @NobbZ is correct you forgot to add

alias MyApp.Accounts.User

so for eample my_app becomes MyApp

Also MyApp is the namespace for the model

Thank you! This is exactly where my lack of understanding came from. Within the file, the module name/path is


For some reason, I thought we didn’t need to provide TestingLocation since that was the name of the application. But when trying to us an Alias, I should use the entire path written in the module in order to gain access to it.

Thank you so much for helping me better understand this part when using alias.

Thank you so much! both you and @NobbZ. As I mentioned in a previous post, I thought we didn’t need to use our application name earlier but finally understood it is require when we want to alias it.

Thank you for that explanation in regards to MyApp being the namespace and what the appropriate way to alias a module is.

You are welcome, also best of luck in your elixir phoenix development and learning

This is probably not your applications name. but your top-level “namespace”. The application name is usually a lowercased atom with snake case, probably :testing_location.

Also please tick the solution button next to @NobbZ comment regarding the alias so that others can find easier the solution.

I believe the namespace was named after the application name since I used a phx generator as

mix phx.gen.context Accounts User users username:string:unique

This resulted in my module to be named as TestingLocation.Accounts.User although it sounds like we don’t need to follow such a convention of MyApp.Context.Model

I believe I did it correctly. It’s the :white_check_mark: correct? Sorry, this is the first time I’m ticking a solution in the Elixir Forum.

It’s perfect

Great. Thank you :slight_smile: Looking forward to getting better at/learning about Elixir over the coming months -> years.

1 Like