Mix run priv/repo/seeds.exs with multiple string

I want to seed my database with the following data:

for category <- ~w('Drum and Bass' Dubstep Hardcore House Trance Trap) do
	Multimedia.create_category(category)
end

but “drum and bass” gets split into multiple inputs.

SELECT c0."id", c0."name", c0."inserted_at", c0."updated_at" FROM "categories" AS c0 WHERE (c0."name" = $1) ["'Drum"]
[debug] QUERY OK db=2.7ms queue=1.2ms
INSERT INTO "categories" ("name","inserted_at","updated_at") VALUES ($1,$2,$3) RETURNING "id" ["'Drum", ~N[2019-03-21 07:23:01], ~N[2019-03-21 07:23:01]]
[debug] QUERY OK source="categories" db=2.6ms
SELECT c0."id", c0."name", c0."inserted_at", c0."updated_at" FROM "categories" AS c0 WHERE (c0."name" = $1) ["and"]
[debug] QUERY OK source="categories" db=2.3ms
SELECT c0."id", c0."name", c0."inserted_at", c0."updated_at" FROM "categories" AS c0 WHERE (c0."name" = $1) ["Bass',"]
[debug] QUERY OK db=1.2ms queue=1.0ms

Is it possible inserting as one entry?

Because this is how sigil_w works, it splits on whitespace. You can’t change that.

Use a list of strings instead, eg ["Drum and Bass", "Something else"].

3 Likes

Nice! Thanks Nobbz.

This worked:

for category <- ["Drum and Bass", "Dubstep", "Hardcore", "House", "Trance", "Trap"] do
	Multimedia.create_category(category)
end
1 Like