I want to write something like this:
defmodule JobHunt.Repo.Migrations.CompaniesHaveManyJobs do
use Ecto.Migration
def change do
alter table (:companies) do
add :jobs, :has_many, Job
end
end
end
Running mix ecto.migrate
with this migration gives an error, so what is the right way to do this?
Migration. Migrations are used to modify your database schema over time. This module provides many helpers for migrating the database, allowing developers to use Elixir to alter their storage in a way it is database independent.
Ecto is an official Elixir project providing a database wrapper and integrated query language. With Ecto we're able to create migrations, define schemas, insert and update records, and query them. Changesets. In order to insert, update or delete data from the database, Ecto. Repo.
Our you could use this way as the documentation recommends:
alter table(:jobs) do
add :company_id, references(:companies)
end
I'm not sure whether the plural version is required here: references(:companies)
but it did not work for me with phermacy
(singular)
You should add the required foreign key to the jobs table:
defmodule JobHunt.Repo.Migrations.CompaniesHaveManyJobs do
use Ecto.Migration
def change do
alter table(:jobs) do
add :company_id, :integer
end
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With