Can not add new table to existing project

Hello guys. I would like to add another table to my project, however, I can not. I have found out, that it is caused by an already created schema file. This is how looks my migration file:

defmodule Rewardapp.Repo.Migrations.CreateUsers do
  use Ecto.Migration

  def change do
    create table(:users) do
      add :name, :string
      add :surname, :string
      add :role, :string
      add :points, :integer, default: 0
      add :january, :integer, default: 50
      add :february, :integer, default: 50
      add :march, :integer, default: 50
      add :april, :integer, default: 50
      add :may, :integer, default: 50
      add :june, :integer, default: 50
      add :july, :integer, default: 50
      add :august, :integer, default: 50
      add :september, :integer, default: 50
      add :october, :integer, default: 50
      add :november, :integer, default: 50
      add :december, :integer, default: 50
    end
  end
end

And this is user.ex file

defmodule RewardappWeb.User do
  use Ecto.Schema
  import Ecto.Changeset


  schema "users" do
    field :april, :integer
    field :august, :integer
    field :december, :integer
    field :february, :integer
    field :january, :integer
    field :july, :integer
    field :june, :integer
    field :march, :integer
    field :may, :integer
    field :name, :string
    field :november, :integer
    field :october, :integer
    field :points, :integer
    field :role, :string
    field :september, :integer
    field :surname, :string
  end

  @spec changeset(
          {map, map}
          | %{
              :__struct__ => atom | %{:__changeset__ => map, optional(any) => any},
              optional(atom) => any
            },
          :invalid | %{optional(:__struct__) => none, optional(atom | binary) => any}
        ) :: Ecto.Changeset.t()
  @doc false
  def changeset(user, attrs \\ %{}) do
    user
    |> cast(attrs, [:name, :surname, :role, :points, :january, :february, :march, :april, :may, :june, :july, :august, :september, :october, :november, :december])
    |> validate_required([:name, :surname, :role, :points, :january, :february, :march, :april, :may, :june, :july, :august, :september, :october, :november, :december])
  end
end

I would love to add a new table called “awards”, but unfortunately I can not manage to make it. I was trying to create a new table by mixing ecto.gen.# etc., and I was also adding the schema wanted table, mission impossible :frowning:

This is the error, while I added schema of my awards to user.ex

Compiling 1 file (.ex)

== Compilation error in file lib/rewardapp/user.ex ==
** (RuntimeError) schema already defined for RewardappWeb.User on line 6
    lib/rewardapp/user.ex:25: (module)
    (elixir 1.13.3) lib/kernel/parallel_compiler.ex:346: anonymous fn/5 in Kernel.ParallelCompiler.spawn_workers/7

The same error has occurred, when I was trying to drop existing table and create new tables.
So the question is, how can I add the second table? Should I drop in some other way existing tables, or just add some order schema?

The schema ought to look like that:

schema "awards" do
  field :userg, :string
  field :userr, :string
  field :points, :integersss
end

can you post the migration file where you created the awards table?

Also, can you post the full file where you define

schema "awards" do
  field :userg, :string
  field :userr, :string
  field :points, :integersss
end
1 Like

This error doesn’t make sense with line 25 of the user.ex above, but I think I understand what’s actually there: the schema "awards" do line mentioned later.

In Ecto, one schema is one module. You’ll want to either make your own award.ex file, or the generators will do it for you.

1 Like

So how could I create this module? And this specific shema?

have you tried this? (based on your schema)

mix phx.gen.schema Award awards userg:string userr:string points:integer

mix.gen.schema

1 Like

That’s a little bit funny, I have tried the same command before and it did not work, but right now it works correctly :smiley: Thank you!!!