Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phoenix seeding dates

Can anyone help with what must be a silly mistake I fear. Consider these two snippets:

defmodule MosaicApi.Repo.Migrations.CreateCard do
  use Ecto.Migration

  def change do
    create table(:cards) do
      add :creation_date, :date

and

defmodule MosaicApi.Card do
  use MosaicApi.Web, :model

  schema "cards" do
    field :creation_date, Ecto.Date

I'm trying to seed some data using

cards = [
    %Card{
        creation_date: "2014-04-17",

I got these simple strings from things I found on the Internet; the alternative seemed to be {"2015", "04", "17"}.

** (Ecto.ChangeError) value "2014-04-17" for MosaicApi.Card.creation_date in insert does not match type Ecto.Date

like image 865
Simon H Avatar asked Apr 26 '16 11:04

Simon H


1 Answers

Try this:

cards = [
  %Card{
    creation_date: %Ecto.Date{year: 2014, month: 4, day: 17},

Or:

cards = [
  %Card{
    creation_date: Ecto.Date.cast!("2014-04-17"),

The generators for the tests were recently changed from the string format (which could be where you got the idea) https://github.com/phoenixframework/phoenix/commit/badf8065d0b025d6b6ad212bfa0b96e897f06a6a

like image 75
Gazler Avatar answered Nov 15 '22 10:11

Gazler