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

Inserting objects without ID #62

Closed
dricholm opened this issue Jul 5, 2019 · 6 comments
Closed

Inserting objects without ID #62

dricholm opened this issue Jul 5, 2019 · 6 comments

Comments

@dricholm
Copy link

dricholm commented Jul 5, 2019

Refers to the documentation: https://moor.simonbinder.eu/queries/

I have a question about inserts.
In the documentation Todo() is used for creating the object.

Future<int> addTodoEntry(Todo entry) {
  return into(todos).insert(entry);
}

addTodoEntry(
  Todo(
    title: 'Important task',
    content: 'Refactor persistence code',
  ),
);

When I create an object without ID (autoIncrement() is used), the IDE (VSCode) will display a warning:
The parameter 'id' is required.dart(missing_required_param).
It seems moor_generator also attaches @required to the ID, even though it is using autoIncrement(). Although this will not throw an error during runtime. When omitting other columns it will result in a runtime error.
Could this be changed to not display the warnings?

Also I noticed in your gist from before that you used a Companion also when inserting.
Should I also use Companion object when inserting? This would probably solve the warning problem mentioned above. Or should it only be used for updates?

Current pubspec.lock:

moor:
  dependency: "direct overridden"
  description:
    path: "moor/"
    ref: develop
    resolved-ref: d98449407e1f170487fd0a1990d29d7b3ddeb93e
    url: "https://github.com/simolus3/moor.git"
  source: git
  version: "1.5.1+1"
moor_flutter:
  dependency: "direct main"
  description:
    name: moor_flutter
    url: "https://pub.dartlang.org"
  source: hosted
  version: "1.5.0"
moor_generator:
  dependency: "direct dev"
  description:
    name: moor_generator
    url: "https://pub.dartlang.org"
  source: hosted
  version: "1.5.0"

Thank you!

@simolus3
Copy link
Owner

simolus3 commented Jul 5, 2019

Yes, use companions! The documentation should have pointed this out, so let's keep this issue open until I fix the docs. Edit: It's fixed on develop, will release another versions of the docs when the next version comes out.

@simolus3 simolus3 closed this as completed Jul 5, 2019
@NovaLogicDev
Copy link

Ok, so now I'm. confused in the docs it says to create the setter to use a companion as the parameter but when I call the setter I should use the normal DataClass? Doing so gives type mismatches and I cant seem to construct the companion in the same way as demonstrated in the update section as Value() is not found by dart.

@simolus3
Copy link
Owner

simolus3 commented Jul 18, 2019

@NovaLogicDev You should use companions for all inserts or updates. Using the regular data classes will work as well, but only for backwards compatibility.

I just realized that the docs use a TodosCompanion as a parameter and then pass the data class Todo directly below it 🤦‍♂️ That's a mistake, will fix.

If you import moor_flutter.dart, the Value class should be found, its definitely exported. It could be that you import another library that has a clashing Value class. Can you give me the exact error when using the Value class? And just to be sure, you're using moor >= 1.5 right (the exact version will be written in pubspec.lock)

@NovaLogicDev
Copy link

NovaLogicDev commented Jul 18, 2019

Just to be clear I should Import moor on main.dart/wherever I use the dbo as well?

@simolus3
Copy link
Owner

Everywhere you use construct a Value, yes. It's part of moor, so you would have to import it. You could also put this:

export 'package:moor_flutter/moor_flutter.dart' show Value;

in your database dart file, then you don't have to import moor everywhere else.

@NovaLogicDev
Copy link

Ah that's a neat trick, cool that fixed all the issues. Thanks for the quick response

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

3 participants