I have a Phoenix app which has schema's setup and is utilising a pre-existing database with data.
I came across quite an unhelpful error message while attempting to create tests for the Phoenix application.
When running mix test
** (ArgumentError) cannot load `"2"` as type :integer
(ecto) lib/ecto/repo/queryable.ex:132: Ecto.Repo.Queryable.load!/3
(ecto) lib/ecto/adapters/sql.ex:515: anonymous fn/3 in Ecto.Adapters.SQL.process_row/3
(elixir) lib/enum.ex:1151: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
(ecto) lib/ecto/adapters/sql.ex:513: Ecto.Adapters.SQL.process_row/3
(elixir) lib/enum.ex:1088: Enum."-map/2-lists^map/1-0-"/2
(ecto) lib/ecto/adapters/sql.ex:283: Ecto.Adapters.SQL.decode/2
(ecto) lib/ecto/adapters/sql.ex:272: Ecto.Adapters.SQL.decode/3
(ecto) lib/ecto/adapters/sql.ex:225: Ecto.Adapters.SQL.query/5
At first I had no idea what was going on and assumed I'd some something wrong in my Elixir code.
I eventually had a look at test_helper.exs and noticed this line:
Mix.Task.run "ecto.migrate", ~w(-r AdminPhoenix.Repo --quiet)
When I run mix ecto.migrate outside of mix test, I obviously receive the same message.
This is potentially quite an odd edge case as most I'd assume are building brand new applications. Though I'd imagine I won't be the only one who wants to build a Phoenix application for an existing database :)
I'm not sure whether it would warrant a change in Ecto for ecto.migrate to tell the user that there are no migrations present, as even that would have helped significantly.
It's not clear in the issue where the error is coming from. Did removing Mix.Task.run "ecto.migrate" resolve it? If you run ecto.migrate without any migrations it should be a noop, so if anything is breaking it's an ecto bug.
I don't think it is a migration issue as well. You have specified something as type :integer in your model but it is stored as a string "2" in your database. You need to make sure the types message. I will improve the error message to include the model if possible.
Removing ecto.migrate from the test helper fixes it. When I run ecto.migrate manually in the context of mix, it produces the exact same error message.
It makes sense that it could be a type issue. I will try and hunt this down and report back whether it's me being silly
@josevalim - I've tested this deeper and found that it's nothing to do with my schema's, it's actually to do with the database I'm connecting to!
I created a blank new phoenix app with no migrations or schemas, pointed it to this existing database and ran mix ecto.migrate
It turns out it's Ecto not playing well with the fact the database I'm reading from is a Rails app DB - which has a "schema_migrations" table, which stores migrations as strings rather than int. So what it's actually doing is reading the schema_migrations records and trying to parse them.
Hopefully this is of some help?
We should do a sanity check on the format of the schema_migrations table and if doesn't match what we expect raise with an explanation.
Format checking would be a pain to do. You can either change your schema migrations table to an integer (Rails would likely be fine with that) or you can rename Ecto to use another table (not what you want) or you can simply not run migrations from the Ecto side.
No problem, I figured it might be a good idea to flag this up as there'll be potentially quite a few people wanting to migrate from Rails to Phoenix and come across this same issue. I guess at least now this issue is discoverable!
Format checking would be a pain to do.
Fair enough. I thought it would be easy since I believe we do some table checks in migrations? But if it's hard it's not worth it.
Maybe we could instead rescue errors when accessing schema_migrations and add a warning that something in schema_migrations could be incorrect? More will have this issue since rails uses the same table.
@ericmj i have changed the error to include the schema, so next time this error appears it will be obvious. We can investigate if it is not enough.
It wasn't obvious for me :-) I had a error message exactly as above (just with a different number). It took me an hour to find this thread.
@ckruse can you please open up an issue on https://github.com/elixir-ecto/ecto? I will make sure to make it less confusing.
Done. Thanks for the great work!
Most helpful comment
@ckruse can you please open up an issue on https://github.com/elixir-ecto/ecto? I will make sure to make it less confusing.