question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Postgres Identity column does not work

See original GitHub issue

Describe the bug The identity option, as well the the default sequential id value do not work with postgres

To Reproduce

            services.AddFluentMigratorCore()
                .ConfigureRunner(fluentMigratorBuilder => fluentMigratorBuilder
                    .AddPostgres()
                    .WithGlobalConnectionString(connectionString)
                    .ScanIn(AssemblyWithMigrations).For.Migrations()
                )
                .AddLogging(lb => lb.AddFluentMigratorConsole());

With the uuid-ossp installed as an extension on my postgres database the following migration results in an integer primary key:

Create.Table("Players")
.WithColumn("Id").AsGuid().NotNullable().PrimaryKey().Identity()
.WithColumn("FirstName").AsString()

This results in a PK uuid which seems promising:

Create.Table("Players")
.WithColumn("Id").AsGuid().NotNullable().PrimaryKey().WithDefault(SystemMethods.NewSequentialId)
.WithColumn("FirstName").AsString()

however when I attempt the following query: insert into "Players" VALUES ('George')

I get the following error

ERROR:  invalid input syntax for type uuid: "George"
LINE 1: insert into "Players" VALUES ('George')

It seems then that I cannot have an auto generating uuid.

an int identity also doesnt work:

Create.Table("Players")
                .WithColumn("Id").AsInt32().NotNullable().PrimaryKey().Identity()
                .WithColumn("FirstName").AsString().NotNullable();

results in a primary key but not an identity column.

ERROR:  invalid input syntax for type integer: "George"
LINE 1: insert into "Players" VALUES ('George')

Expected behavior An identity column should be generated

Information (please complete the following information):

  • OS: [ Windows 10 ]
  • Platform [.Net Core 5 / PostgreSQL 13]
  • FluentMigrator version [ 3.2.15 ]
  • FluentMigrator runner [ “in-process runner” ]
  • Database Management System [ Postgres ]
  • Database Management System Version [ “pgAdmin 4” ]

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
jzabroskicommented, Mar 31, 2021

@lillo42 can you please look

0reactions
CraigComeOnThisNameCantBeTakencommented, Apr 2, 2021

@CraigComeOnThisNameCantBeTaken the problem is your insert query.

The correct query:

insert into "Players" ("FirstName") VALUES ('George') 

When you create a table with default, that means if you don’t pass a value Postgres will use the default value, also in your query you don’t explicit the column, so PostgreSQL is assuming you are going to pass all column.

About this part:

Create.Table("Players")
    .WithColumn("Id").AsGuid().NotNullable().PrimaryKey().Identity()

It doesn’t make sense you use AsGuid and Identity together because Identity is for int or long only.

And about this:

Create.Table("Players")
                .WithColumn("Id").AsInt32().NotNullable().PrimaryKey().Identity()
                .WithColumn("FirstName").AsString().NotNullable();

We generated this query:

create table "Players"
(
    "Id" serial not null primary key,
    "FirstName" text not null
);

Postgres translate it for:

create table "Players" (
    "Id" integer NOT NULL DEFAULT nextval('Players_id_seq'),
    "FirstName" text not null
);

So you need to specific the column too.

For insert query the best thing is use explicit column, I always do it.

Im very sorry! Im sorry I wasted your time and thanks for the help. This was 10/10 stupid.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PostgreSQL unable to set existing column as IDENTITY
Just to clarify what the documentation says: Don't use serial. For new applications, identity columns should be used instead.
Read more >
PostgreSQL Identity Column
This tutorial shows you how to use the GENERATED AS IDENTITY constraint to create the PostgreSQL identity column for a table.
Read more >
PostgreSQL 10 identity columns explained
One common problem is that permissions for the sequence created by a serial column need to be managed separately:
Read more >
Restarting identity columns in Postgresql
From version 10, using identity columns, there is no need to use the sequence name. That's nice. ALTER TABLE table ALTER COLUMN id...
Read more >
PostgreSQL: Create Identity Column in a Table
In PostgreSQL, the identity column is a NOT NULL column that has an implicit sequence attached to it and the column in new...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found