0

I am new to Elixir/Phoenix/Ecto. I created a database with 4 different schemas using ecto.migration. The tables were created correctly, but when I try to query the tables, ecto doesn't find them.

ERROR 42P01 (undefined_table): relation "users" does not exist

I know the problem is database schema-related because when I create the users table in the default schema, ecto finds it without any problem.

3
  • make sure to migrate (mix ecto.migrate) before you can use the tables. Commented Jul 29, 2018 at 6:42
  • Try adding a @schema_prefix annotation to the Ecto schema declaration. See see @schema_prefix section in hexdocs.pm/ecto/Ecto.Schema.html#module-schema-attributes for more details.
    – Dogbert
    Commented Jul 29, 2018 at 17:14
  • @Dogbert. You are correct and your answer was specific enough to be helpful. If you make this an answer, I will accept it. Commented Jul 30, 2018 at 12:16

1 Answer 1

1

Make sure the migration file and the schema file have the same table name "users". You can use the mix ecto.migrations command to check the status of each migration file to make sure it was applied to the database.

In Phoenix, you don't have to use Ecto directly to create schemas. You can use phoenix commands to help you create the necessary files and modules:

mix phx.gen.html Accounts User users username:string age:integer

This will create a migration file which will create a "users" table. It will also create a schema file and module called MyApp.Accounts.User which will be tied to the users table through the schema "users" do macro.

If you are creating a migration file manually, make sure you are using the correct table name everywhere, i.e., in queries and schema files.

2
  • 1
    Your answer, I feel, isn't quite specific enough. The problem was that in my Repo.all() and other functions, I needed to include prefix: 'accounts.' I don't want to answer my own question, so if you would like to improve your answer, I will accept it. Commented Jul 31, 2018 at 11:08
  • To be fair, @Dogbert answered the question correctly so credit should go to him. It's amazing honestly how he pin-pointed the problem. I thought phoenix removed the schema prefixes before 1.3 final was released. Commented Jul 31, 2018 at 11:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.