Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bindInsert doesn't support multiple autoincrement columns #393

Open
siddharth178 opened this issue Apr 12, 2019 · 3 comments
Open

bindInsert doesn't support multiple autoincrement columns #393

siddharth178 opened this issue Apr 12, 2019 · 3 comments

Comments

@siddharth178
Copy link

I am on PostgreSQL, and have a table with 2 auto increment columns along with other columns. One of those auto increment columns is PK.

What I was expecting is, calling SetKeys on PK and then setting autoincrement tag on 2nd auto increment column should make it work properly - i.e. insert should insert nextvals in both those columns and object that I am inserting should get those values reflected. But it looks like

if plan.autoIncrIdx > -1 {
doesn't handle multiple autoincrement columns. It only generates returning clause with latest autoincrement column.

Any specific reason to not include all autoincrement columns as part of returning clause?

@nelsam
Copy link
Member

nelsam commented Apr 19, 2019

This is probably an artifact of trying to support a bunch of databases, many of which don't conform well to the SQL standard. For example: MySQL does not have a returning clause.

If you would like to make a PR to fix it, you're welcome to!

@siddharth178
Copy link
Author

siddharth178 commented Apr 29, 2019

What I am thinking of to fix this is

  • Main idea here is to get autoincrement struct field tag working when there is another column set as PK.
  • Change Dialect's AutoIncrInsertSuffix method to accept slice of column names

  • Other than Postgresql implementation all other implementations will still return "" - empty string

    • Postgres implementation will return returning clause with all the column names that are passed in as argument
  • Change TableMap.bindInsert to keep track of all the column names in a separate string slice for which autoincrement is needed/requested

    • Then pass that string slice to AutoIncrInsertSuffix

@siddharth178
Copy link
Author

Looking more into the code I realised that above approach will only modify the query to return multiple values. But I will also need to update insert and handle cases like TargetQueryInserter, TargetedAutoIncrInserter, IntegerAutoIncrInserter etc. which are currently written to handle single autoincrement field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants