I have a Users table like:
email | username
---------------+----------
[email protected] |
[email protected] |
[email protected] |
and I want to update username field by email field, just slice the email before @.
email | username
---------------+----------
[email protected] | 123
[email protected] | 123
[email protected] | haha
I have try to use the following migration:
defmodule MyApp.Repo.Migrations.AddDefaultUsernameForUsers do
use Ecto.Migration
import Ecto.Query
def up do
from(u in MyApp.User, update: [set: [username: String.split(u.email, "@") |> List.first ]])
|> MyApp.Repo.update_all([])
end
def down do
MyApp.Repo.update_all(MyApp.User, set: [username: nil])
end
end
But when runing the migration, I got the following error:
$ mix ecto.migrate
** (Ecto.Query.CompileError) `List.first(String.split(u.email(), "@"))` is not a valid query expression
How can I solve this?