Phx.gen missing primary keys and bad types in references

i’m not getting primary keys for phx.gen.html/json.

mix phx.gen.html Content Community communities name:string:unique description:string user_id:references:users

resulted in

  def change do
  │ create table(:communities) do
  │ │ add :name, :string
  │ │ add :description, :string
  │ │ add :user_id, references(:users, on_delete: :nothing)

  │ │ timestamps()
  │ end

    create unique_index(:communities, [:name])                                                                                                                                
  │ create index(:communities, [:user_id])

without any primary keys entirely, …and the references ignores the uuid type that is on the referenced users table. For users table I used --binary-id and got add :id, :binary_id, primary_key: true

am I expected to always use either --no-binary-id or --binary-id to get primary keys?

trying to run this, i get

ERROR 42804 (datatype_mismatch) foreign key constraint “communities_user_id_fkey” cannot be implemented

Key columns “user_id” and “id” are of incompatible types: bigint and uuid.

and it is not clear how i can even force :uuid types in a reference…

missing from docs but easy to find on source. the fix for the wrong reference type is to add type: parameter.

add :user_id, references(:users, on_delete: :nothing, type: :binary_id) 

but still clueless about the lack of primary keys

heh. the simple id is hidden on the create tables. i guess it is all good then.

1 Like