gorm icon indicating copy to clipboard operation
gorm copied to clipboard

Is it possible to turn off getting the last inserted id?

Open ppanphper opened this issue 2 years ago • 10 comments

Your Question

If returning id is turned off, and the model contains an ID field, lastInsertId will be read by default; eg: driver postgres WithoutReturning: true

gorm.Model contain ID field

The document you expected this should be explained

Expected answer

Is it possible to turn off this feature? image

ppanphper avatar May 22 '23 08:05 ppanphper

We no longer support this feature, refer to https://github.com/go-gorm/postgres/issues/177#issuecomment-1522815541 Does anyone else need to support this feature? I hope to get more feedback. To support this feat, a configuration can be added to disable LastInsertId for drivers.

a631807682 avatar May 22 '23 15:05 a631807682

Maybe we can use field permission to disable the LastInsertId feature?

jinzhu avatar May 23 '23 02:05 jinzhu

What are the scenarios? Can you describe it in detail? @ppanphper My understanding is

  1. Some databases do not support either Returning syntax or LastInsertID queries, in which case we need to disable them at the driver level
  2. LastInsertID causes other problem in some cases, so we need to disable it, in which case we can provide field permission disable or driver level disable. https://github.com/go-gorm/gorm/issues/6047

a631807682 avatar May 23 '23 02:05 a631807682

@a631807682

eg: distributed pgsql database dws;

For distributed database dws, if you want to return id, sql cannot be pushed down to DN, which affects performance.

If possible, it is best to support control at the driver and field levels; in fact, it is also possible to restrict the characteristics of the default primary key of id.

ppanphper avatar May 23 '23 03:05 ppanphper

I am facing the same problem, I try to operate aws redshift, which can't support returning id, when I disable it , it return an error: "LastInsertId is not supported by this driver".

yyystation avatar Apr 17 '24 03:04 yyystation

May I take this one?

e1esm avatar Jun 18 '24 16:06 e1esm

May I take this one?

Assigned to you, thanks.

a631807682 avatar Jun 19 '24 01:06 a631807682

is there any update on this issue @e1esm ? I'm also facing same problem for redshift.

I try to operate aws redshift, which can't support returning id, when I disable it , it return an error: "LastInsertId is not supported by this driver".

Ajaysinh1290 avatar Jan 15 '25 08:01 Ajaysinh1290

If this issue is not fixed, I would do it in 7 days. @e1esm @a631807682

krisdiano avatar May 29 '25 06:05 krisdiano

Need reviewers. @a631807682

krisdiano avatar May 29 '25 12:05 krisdiano