diff --git a/lib/bones73k/accounts/user.ex b/lib/bones73k/accounts/user.ex index 6e69294..8d67ee0 100644 --- a/lib/bones73k/accounts/user.ex +++ b/lib/bones73k/accounts/user.ex @@ -16,6 +16,8 @@ defmodule Bones73k.Accounts.User do @max_password 80 @derive {Inspect, except: [:password]} + @primary_key {:id, :binary_id, autogenerate: true} + @foreign_key_type :binary_id schema "users" do field :email, :string field :password, :string, virtual: true diff --git a/lib/bones73k/accounts/user_token.ex b/lib/bones73k/accounts/user_token.ex index ba3911e..7114f32 100644 --- a/lib/bones73k/accounts/user_token.ex +++ b/lib/bones73k/accounts/user_token.ex @@ -12,6 +12,8 @@ defmodule Bones73k.Accounts.UserToken do @change_email_validity_in_days 7 @session_validity_in_days 60 + @primary_key {:id, :binary_id, autogenerate: true} + @foreign_key_type :binary_id schema "users_tokens" do field :token, :binary field :context, :string diff --git a/lib/bones73k/properties/property.ex b/lib/bones73k/properties/property.ex index 6293542..97135e1 100644 --- a/lib/bones73k/properties/property.ex +++ b/lib/bones73k/properties/property.ex @@ -2,11 +2,13 @@ defmodule Bones73k.Properties.Property do use Ecto.Schema import Ecto.Changeset + @primary_key {:id, :binary_id, autogenerate: true} + @foreign_key_type :binary_id schema "properties" do field :description, :string field :name, :string field :price, :decimal - field :user_id, :id + belongs_to :user, Bones73k.Accounts.User timestamps() end diff --git a/priv/repo/migrations/20200913000515_create_users_auth_tables.exs b/priv/repo/migrations/20200913000515_create_users_auth_tables.exs index bdcc744..efc5f61 100644 --- a/priv/repo/migrations/20200913000515_create_users_auth_tables.exs +++ b/priv/repo/migrations/20200913000515_create_users_auth_tables.exs @@ -4,7 +4,8 @@ defmodule Bones73k.Repo.Migrations.CreateUsersAuthTables do def change do execute("CREATE EXTENSION IF NOT EXISTS citext", "") - create table(:users) do + create table(:users, primary_key: false) do + add(:id, :binary_id, primary_key: true) add(:email, :citext, null: false) add(:hashed_password, :string, null: false) add(:confirmed_at, :naive_datetime) @@ -13,8 +14,9 @@ defmodule Bones73k.Repo.Migrations.CreateUsersAuthTables do create(unique_index(:users, [:email])) - create table(:users_tokens) do - add(:user_id, references(:users, on_delete: :delete_all), null: false) + create table(:users_tokens, primary_key: false) do + add(:id, :binary_id, primary_key: true) + add(:user_id, references(:users, type: :binary_id, on_delete: :delete_all), null: false) add(:token, :binary, null: false) add(:context, :string, null: false) add(:sent_to, :string) diff --git a/priv/repo/migrations/20200914162043_create_properties.exs b/priv/repo/migrations/20200914162043_create_properties.exs index 97f3ec0..d93506c 100644 --- a/priv/repo/migrations/20200914162043_create_properties.exs +++ b/priv/repo/migrations/20200914162043_create_properties.exs @@ -2,11 +2,12 @@ defmodule Bones73k.Repo.Migrations.CreateProperties do use Ecto.Migration def change do - create table(:properties) do + create table(:properties, primary_key: false) do + add(:id, :binary_id, primary_key: true) add(:name, :string) add(:price, :decimal) add(:description, :text) - add(:user_id, references(:users, on_delete: :nothing)) + add(:user_id, references(:users, type: :binary_id, on_delete: :nothing)) timestamps() end diff --git a/priv/repo/seeds.exs b/priv/repo/seeds.exs index 792cffa..72f4671 100644 --- a/priv/repo/seeds.exs +++ b/priv/repo/seeds.exs @@ -10,10 +10,10 @@ # We recommend using the bang functions (`insert!`, `update!` # and so on) as they will fail if something goes wrong. +import Ecto.Query alias Bones73k.Repo alias Bones73k.Accounts alias Bones73k.Accounts.User -alias Bones73k.Properties alias Bones73k.Properties.Property ############################################################################ @@ -114,15 +114,18 @@ count_to_take = 123 mock_props = props_json |> File.read!() |> Jason.decode!() |> Enum.take_random(count_to_take) +random_user_query = from User, order_by: fragment("RANDOM()"), limit: 1 + mock_props = Enum.map(mock_props, fn e -> add_dt = NaiveDateTime.from_iso8601!(e["inserted_at"]) + rand_user = Repo.one(random_user_query) %{ name: e["name"], price: e["price"], description: e["description"], - user_id: e["user_id"], + user_id: rand_user.id, inserted_at: add_dt, updated_at: add_dt }