title: Ecto: How to validate presence of belongs_to association?
body: <pre><code>defmodule ParrotApi.Meetup do
  
      schema "meetups" do
        field :timestamp, :integer
        belongs_to :topic, ParrotApi.Topic
  
        timestamps()
      end
  
  
      @required_fields ~w(timestamp)
  
      def changeset(struct, params \\ %{}) do
        struct
        |&gt; cast(params, @required_fields)
        |&gt; cast_assoc(:topic, required: true)
        |&gt; validate_required(@required_fields)
      end
  
  end
  </code></pre>
  
  <p>But when I do this in my tests:</p>
  
  <pre><code>%Meetup{timestamp: future_time, topic_id: topic.id} |&gt; Repo.insert
  </code></pre>
  
  <p>it doesn't through an error:</p>
  
  <pre><code>iex(10)&gt; %Meetup{timestamp: 123123} |&gt; Repo.insert
  [debug] QUERY OK db=0.3ms
  begin []
  [debug] QUERY OK db=1.3ms
  INSERT INTO "meetups" ("timestamp","inserted_at","updated_at") VALUES ($1,$2,$3) RETURNING "id" [123123, {{2017, 4, 13}, {12, 3, 24, 644065}}, {{2017, 4, 13}, {12, 3, 24, 644074}}]
  [debug] QUERY OK db=1.6ms
  commit []
  {:ok,
   %ParrotApi.Meetup{__meta__: #Ecto.Schema.Metadata&lt;:loaded, "meetups"&gt;, id: 6,
    inserted_at: ~N[2017-04-13 12:03:24.644065], timestamp: 123123,
    topic: #Ecto.Association.NotLoaded&lt;association :topic is not loaded&gt;,
    topic_id: nil, updated_at: ~N[2017-04-13 12:03:24.644074]}}
  iex(11)&gt;
  </code></pre>
  
  <p>How do I ensure that Meetups are only made with a topic_id present??</p>