GithubHelp home page GithubHelp logo

LastInsertId method returns 0? about go-pgsql HOT 6 OPEN

lxn avatar lxn commented on May 16, 2024
LastInsertId method returns 0?

from go-pgsql.

Comments (6)

lxn avatar lxn commented on May 16, 2024

This indeed isn't supported. The main obstacle is to get at the name of the primary key sequence.

from go-pgsql.

betamos avatar betamos commented on May 16, 2024

Subscribing, very useful

from go-pgsql.

temoto avatar temoto commented on May 16, 2024

If you use not ancient PostgreSQL, you can do insert into <table> ... returning <id-column> and read it like normal select statement, e.g. Scan().

from go-pgsql.

lxn avatar lxn commented on May 16, 2024

Yes, returning is nice, but to implement LastInsertId, how would we know the exact name of the id column without guessing?

from go-pgsql.

temoto avatar temoto commented on May 16, 2024

I guess there is no way without fetching table structure. And even then you can't do anything for table like id1 serial, id2 serial, primary key (id1, id2).

Basically, LastInsertId interface is not as generic as SQL standard. That's why i'm suggesting him to use other way to get inserted ids.

from go-pgsql.

brianoh avatar brianoh commented on May 16, 2024

I'm using "SELECT LASTVAL()" which appears to work OK, and alternatively a query using eg. " RETURNING iKey" appears to work OK.

from go-pgsql.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.