简体   繁体   中英

Elixir/Phoenix, running into error when adding a row to my 'topics' table in Postgres using Repo.insert(changeset)

I've got a "Topics" context, which defines a single "topics" table in my db, and it only has one field "title", like so:

topic.ex

defmodule Discuss.Topic do
  use Ecto.Schema
  import Ecto.Changeset

  schema "topics" do
    field :title, :string
    timestamps()
  end

  def changeset(topic, attrs) do 
    topic
    |> cast(attrs, [:title]) 
    |> validate_required([:title]) 
  end
end

I run a command in the terminal to create my migration with "mix ecto.gen.migration add_topics", to add a table 'topics' in my db which contains only one 'title' column:

20220728225556_add_topics.exs

defmodule Discuss.Repo.Migrations.AddTopics do
  use Ecto.Migration

  def change do
    create table(:topics) do
      add :title, :string
    end
  end
end

to which I then run "mix ecto.migrate" to execute the migration and make the table in Postgres, which was successful. I now have only one table in my db called 'topics', and it only contains two columns, the id (which is by default), and 'title', which holds string values.

The user can now submit a form containing just one field to add a 'topic' to my topics table, this is done by running a POST request to '/topics' when the form gets submitted.

When my router gets a POST request to '/topics', it runs this "create" function in my topic_controller.ex file, which obtains that 'title' property from the form and applies it to our changeset before we use it to add a new row to my 'topics' table with "Repo.insert(changeset)".

topic_controller.ex:

defmodule DiscussWeb.TopicController do

  use DiscussWeb, :controller 
  
  alias Discuss.Topic
  alias Discuss.Repo

  def new(conn, _params) do
    # the 'conn' struct holds data for the request and the response.
    struct = %Topic{} # this struct starts off empty because we don't have data for the form yet.
    params = %{} 
    changeset = Topic.changeset(struct, params)
    render conn, "new.html", changeset: changeset
  end


  def create(conn, params) do 
    %{"topic" => topic} = params 
# we pattern match a 'topic' variable to get the 'title' property from the form, which I can then view using 'IO.inspect topic'. 
    
    changeset = Topic.changeset(%Topic{}, topic) 
# here, I'm adding my 'title' property to my empty pre-existing changeset, which is successful when I inspect it afterwards.  

    Repo.insert(changeset) 
#this is the function that should be adding a new row to my 'topics' table with the 'title' property we obtained from the form, but instead throws the error I'm experiencing. 
    
  end
end

I'm aware that I'd be given an error for not redirecting my page anywhere anyway, but I should at least still be seeing a new row added to my 'topics' table when the form gets submitted. Instead, I'm given the error you see below saying "ERROR 42703 (undefined_column) column "inserted_at" of relation "topics" does not exist", and even after regenerating my migration and running it a second time, I can't seem to narrow down why my build is telling me that I'm adding a row to a 'topics' table that doesn't exist, when it matter-of-factly, exists. Error seen when adding a row to the 'topics' table.

Proof my 'topics' table exists in my Postgres db

I came to realize that the error wasn't telling me that the table didn't exist, it was that the table was missing columns for 'inserted_at' and 'updated_at'.

As soon as I added 'timestamps()' underneath my 'add:title, :string' in my migration file before I executed it again to rebuild my table, my code began working and I started seeing rows being added to the table with each form submission. PROBLEM SOLVED:)

20220807200421_add_topics.exs:

defmodule Discuss.Repo.Migrations.AddTopics do
  use Ecto.Migration

  def change do
    create table(:topics) do
      add :title, :string
      timestamps()
    end
  end

end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM