Does Go-Gorm support insert with auto incremented ID for partitioned tables in PostgreSQL?

1.7k Views Asked by At

I have a table in PostgreSQL represented as the following Go struct:

type AppLog struct {
    ID int // set to auto increment in DB, also a primary key
    event string
    createTime time.Time 
}

I configured monthly table partitioning with the above as the base table and a insert trigger to route the data into the child table for the current month using the dateTime values as the partition key.

[the trigger function etc are omitted for brevity]

When I try to insert into AppLog table, Postgres routes the operation to the appropriate child table, say AppLog_2017-05 (current month table), but the insert fails with the following error:

INSERT INTO "app_logs" ("event", "create_time") 
VALUES ('device /dev/sdc is now ready','2017-05-26T15:04:30+05:30') 
RETURNING "app_logs"."id"

ERROR: sql: no rows in result set

When the same query is run in the Postgres Shell, it runs fine.

Can someone help me understand how to do inserts using GORM in PostgreSQL where table partitioning is configured behind the scene? I am not sure if this is a problem with GORM or the Go PostgreSQL driver or Go Database/SQL package. Or, if I am missing anything.

Any help will be highly appreciated.

1

There are 1 best solutions below

0
On

I got the answer. It was not an issue with GORM or Go-PQ. I was doing some goof up in my trigger function while returning the inserted values from the child table.