Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

automatically update and create date and time based fields (not a bug) #131

Open
zsan opened this issue Sep 12, 2019 · 3 comments
Open

automatically update and create date and time based fields (not a bug) #131

zsan opened this issue Sep 12, 2019 · 3 comments

Comments

@zsan
Copy link

zsan commented Sep 12, 2019

Hi,

I am starting to use DBIx::Class::Migration, i have followed the tutorials and i have question.

So i have this migration script

use strict;
use warnings;
use DBIx::Class::Migration::RunScript;

migrate {
  my $user_rs = shift->schema->resultset('User');

  $user_rs->create({
    name => 'foo',
    email => '[email protected]',
    # created_at => DateTime->now,
    # updated_at => DateTime->now,
  });
};

And that will fail with message

 DBI Exception: DBD::Pg::st execute failed: ERROR:  null value in column "created_at" violates not-null constraint
DETAIL:  Failing row contains (1, [email protected], foo, null, null). [for Statement "INSERT INTO users ( email, name) VALUES ( ?, ? ) RETURNING id" with ParamValues: 1='[email protected]', 2='foo'] at /Users/zak/Projects/mojolicious/tutorials/minipost/share/migrations/_common/deploy/1/002-demo.pl line 8

and my user's schema

use utf8;
package Minipost::Schema::Result::User;

use strict;
use warnings;

use base 'DBIx::Class::Core';

__PACKAGE__->load_components("TimeStamp");

__PACKAGE__->table("users");

__PACKAGE__->add_columns(
  "id",
  {
    data_type         => "bigint",
    is_auto_increment => 1,
    is_nullable       => 0,
    sequence          => "users_id_seq",
  },
  "email",
  {
    data_type   => "text",
    is_nullable => 1,
    original    => { data_type => "varchar" },
  },
  "name",
  {
    data_type   => "text",
    is_nullable => 1,
    original    => { data_type => "varchar" },
  },
  "created_at",
  {
    data_type => "timestamp",
    is_nullable => 0,
    set_on_create => 1
  },
  "updated_at",
  {
    data_type => "timestamp",
    is_nullable => 0,
    set_on_create => 1,
    set_on_update => 1,
  },
);

__PACKAGE__->set_primary_key("id");

1;

Is there a way to install the migration without having to type the value of created_at and updated_at ?

@rabbiveesh
Copy link
Collaborator

rabbiveesh commented Sep 13, 2019 via email

@zsan
Copy link
Author

zsan commented Sep 13, 2019

You could set a default on the column in your schema. And if you don't want that in general, you could set it at the beginning of the migration and alter the table later.

So actually i have set it on my schema via DBIx::Class::TimeStamp and it failed when i install the migration

@rabbiveesh
Copy link
Collaborator

Does it set the default on the database side? Could you link to a full (non)working example?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants