GithubHelp home page GithubHelp logo

Return last inserted ID/s about floor HOT 7 CLOSED

pinchbv avatar pinchbv commented on July 2, 2024
Return last inserted ID/s

from floor.

Comments (7)

mqus avatar mqus commented on July 2, 2024 1

You can use Future<int> or Future<List<int>> as a return type for @insert DAO functions, e.g.

@insert
Future<int> insertPerson(Person person);

will return a primary id which was generated and used by sqlite.
Of course, this will only work if your primary key is of type integer, but I think that only integer primary keys can be generated anyway. The id(or whatever you named it) field of those Person objects will not have changed, you would have to do that yourself if you want.

See also the README

from floor.

mqus avatar mqus commented on July 2, 2024 1

In the case of @update and @delete it will return the count of changed rows, not the id. This last inserted ID in this ticket only applies to @insert. And as the (admittedly short) issue description says is

Investigate what Room returns on update and delete and decide if support these as well.

And this is (at least from my PoV) the amount of rows that were changed by the operation, just like the titles of the referenced PRs indicate.

If you want the id of your updated row(s), look inside the id field of the Entity you provided to the update function, as this will not be changed(in the db and in your app) by either update nor delete operations.

from floor.

cassioseffrin avatar cassioseffrin commented on July 2, 2024

This feature is already available in last version 0.12.0? If yes, how can I perform it?

from floor.

cassioseffrin avatar cassioseffrin commented on July 2, 2024

In the previous version if I am not wrong the returned int value was 0 or 1 (true or false), just to represent if the insert/update were done/committed without errors. Now it's pretty better providing the accuracy ID information. But seems to be an issue in @update annotation.

In @update the floor API seems to returning always 1 instead of the ID. In @insert it's Ok returning the correct Id.

#personDao.dart
@update
Future updatePerson(Person person);

#instantiating the dao
id = personDao.updatePerson(_person);

In that case, the id is always == 1, instead of the committed _person.id

from floor.

cassioseffrin avatar cassioseffrin commented on July 2, 2024

@mqus ok, it was very clear. Really this way similar to Room got better, thks!

from floor.

mqus avatar mqus commented on July 2, 2024

And honestly, if you had read the README link I gave you, this would have been obvious:

@update marks a method as an update method. When using the capitalized @update you can specify a conflict strategy. Else it just defaults to aborting the update. These methods can return a Future of either void or int.

void return nothing
int return number of changed rows

@delete marks a method as a deletion method. These methods can return a Future of either void or int.

void return nothing
int return number of deleted rows

from floor.

cassioseffrin avatar cassioseffrin commented on July 2, 2024

Yeap indeed. I didn't understand correctly. It's very clear in docs!

from floor.

Related Issues (20)

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.