diff --git a/lib/DBIx/Class/Schema/Loader/DBI/Pg.pm b/lib/DBIx/Class/Schema/Loader/DBI/Pg.pm index 7dd911c06..2c7b28dba 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/Pg.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/Pg.pm @@ -198,6 +198,7 @@ sub _columns_info_for { my ($table) = @_; my ($result, $raw) = $self->next::method(@_); + my %pkeys; while (my ($col, $info) = each %$result) { my $data_type = $info->{data_type}; @@ -342,6 +343,17 @@ EOF # PostgreSQL v10 upcases current_timestamp in default values ${ $info->{default_value} } =~ s/\b(CURRENT_TIMESTAMP)\b/lc $1/ge; } + + # if there's a default value + it's a primary key, set to retrieve the default + # on insert even if it's not serial specifically + if (!$info->{is_auto_increment}) { + %pkeys = map { $_ => 1 } @{ $self->_table_pk_info($table) } unless %pkeys; + + if ($pkeys{$col}){ + $info->{retrieve_on_insert} = 1 + } + + } } # detect 0/1 for booleans and rewrite diff --git a/t/10_03pg_common.t b/t/10_03pg_common.t index 2fe5fc9b8..9e3bf0b49 100644 --- a/t/10_03pg_common.t +++ b/t/10_03pg_common.t @@ -288,6 +288,14 @@ dbixcsl_common_tests->new( name ) where active }, + q{ + create table pg_loader_test13 ( + created DATE PRIMARY KEY DEFAULT now(), + updated DATE DEFAULT now(), + type text, + value integer + ) + } ], pre_drop_ddl => [ 'DROP SCHEMA dbicsl_test CASCADE', @@ -296,8 +304,8 @@ dbixcsl_common_tests->new( 'DROP TYPE pg_loader_test_enum', 'DROP VIEW pg_loader_test11', ], - drop => [ qw/pg_loader_test1 pg_loader_test2 pg_loader_test9 pg_loader_test10 pg_loader_test12/ ], - count => 11 + 33 * 2, # regular + multi-schema * 2 + drop => [ map "pg_loader_test$_", 1, 2,9, 10, 12, 13 ], + count => 13 + 33 * 2, # regular + multi-schema * 2 run => sub { my ($schema, $monikers, $classes) = @_; @@ -512,6 +520,13 @@ dbixcsl_common_tests->new( { $schema->source($monikers->{pg_loader_test12})->unique_constraints }, { pg_loader_test12_value => ['value'] }, 'unique indexes are dumped correctly'; + + my $pg_13 = $schema->source($monikers->{pg_loader_test13}); + is $pg_13->column_info('created')->{retrieve_on_insert}, 1, + 'adds roi for primary key col w/ non serial default'; + + is $pg_13->column_info('updated')->{retrieve_on_insert}, undef, + 'does not add roi for non-primary keys with a default'; }, }, )->run_tests();