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.

InsertAsync on Postgres returns the wrong identifier

See original GitHub issue

Bug Description

I have a table in Postgres with the following structure:

image

which is then mapped to the following model:

image

since upgrading to verison 1.13.0 InsertAsync has started returning an empty GUID for the identifier:

image

It works fine if the account used to connect to the database is a member of the super user role or is the owner of the table but fails when I use a normal user account that is not the owner which makes me think it might have something to do with information schema permissions. The same code worked using the previous version of RepoDb.

Interestingly the synchronous version of Insert does not seem to be effected.

Additional information:

I believe RepoDb is executing the following SQL internally to get informaiton about the table:

SELECT    c.column_name ,          Cast((          CASE
                    WHEN c.column_name = tmp.column_name THEN 1                    ELSE 0          END) AS BOOLEAN) AS isprimary ,          CASE
                    WHEN c.is_identity = 'YES'                    OR        position('NEXTVAL' IN upper(c.column_default)) >= 1 THEN true
                    ELSE false
          END                            AS isidentity ,          cast(c.is_nullable AS boolean) AS isnullable ,          c.data_type                    AS datatype ,          CASE
                    WHEN c.column_default IS NOT NULL THEN true
                    ELSE false
          END AS hasdefaultvalue
FROM      information_schema.columns c
LEFT JOIN          (                 SELECT c.table_schema ,                        c.table_name ,                        c.column_name ,                        c.column_default
                 FROM   information_schema.table_constraints tc
                 JOIN   information_schema.constraint_column_usage AS ccu
                 using  (constraint_schema, constraint_name)                 JOIN   information_schema.columns AS c
                 ON     c.table_schema = tc.constraint_schema
                 AND    tc.table_name = c.table_name
                 AND    ccu.column_name = c.column_name
                 WHERE  tc.constraint_type = 'PRIMARY KEY' ) tmp
ON        tmp.table_schema = c.table_schema
AND       tmp.table_name = c.table_name
AND       tmp.column_name = c.column_name
WHERE     c.table_name = 'asset_type'AND       c.table_schema = 'public';`

when I execute this as a superuser I get:

MicrosoftTeams-image (11)

but when I execute the same query as a normal user i get the following results:

MicrosoftTeams-image (10)

Issue Analytics

  • State:open
  • Created 7 months ago
  • Reactions:1
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
fenilrathodcommented, Feb 15, 2023

@mikependon I’ve also faced the same issue. As @grofit said, it is related to the user’s table/column ownership only.

The issue is occurring, when using a DB user (who is not a DB Owner) having only read/write rights on specific tables. This is because of information_schema.column view. It shows only those columns that the current user has access to (by way of being the owner or having some privilege). Documentation

In the end, we directly use the user who was DB Owner to resolve the issue as we didn’t have much time to investigate further.

1reaction
mikependoncommented, Feb 14, 2023

In the latest version, that class and also to the other extended libraries (i.e.: RepoDb.SqlServer, RepoDb.MySql, etc), they were all marked as obsolete already.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Remove "RETURNING *" or "RETURNING Id" in Dapper. ...
My problem is InsertAsync adds RETURNING Id at the end of my query. But my column name is "Id" with quotes. But when...
Read more >
sql - Return id from postgresql insertion
There are several ways to do it: Assuming a._id is a serial column: insert into a (data) values ('foo'); insert into b (_id, ......
Read more >
Dapper Insert, Insert Async, Multiple Rows, Bulk Insert
How to insert data and return the id in Dapper? To insert data and return the id, you have 2 choices: Use the...
Read more >
Documentation: 15: INSERT
The optional RETURNING clause causes INSERT to compute and return value(s) based on each row actually inserted (or updated, if an ON CONFLICT...
Read more >
items":[{"name":"query","path":"packages/knex/src ... - Luftung
Batch inserts only work on Postgres because Postgres is the only database engine that returns the identifiers of all inserted rows. js which...
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