jaguar_orm icon indicating copy to clipboard operation
jaguar_orm copied to clipboard

Add tests to make sure sqflite composer is solid

Open tejainece opened this issue 5 years ago • 13 comments

  • [x] int, String, double, bool, DateTime fields
  • [x] Autoincrement
  • [ ] Unique columns
  • [ ] Compound primary keys
  • [ ] Foreign keys
  • [ ] Joins

tejainece avatar Jul 05 '18 16:07 tejainece

@jaumard Have tested all different field types: https://github.com/jaguar-orm/sqflite/blob/master/lib/post.dart

tejainece avatar Jul 05 '18 17:07 tejainece

Cool !! Are you able to read them back correctly ? Because on my PR I guess it was for the save, but once you read back the values bool and dateTime need to be converted back from integer 0/1 (maybe automatic this one) and text to datetime

jaumard avatar Jul 05 '18 18:07 jaumard

Yes. I have added readback capability using parseValue method on Adapter.

Try the example project.

tejainece avatar Jul 05 '18 18:07 tejainece

I'll for sure :) but can't right now and I was curious :D thanks !

jaumard avatar Jul 05 '18 18:07 jaumard

@tejainece I'm going to test right now, but I just saw this on the doc of sqflit https://github.com/tekartik/sqflite/blob/master/doc/opening_db.md interesting part is that only one database can be opened (all other will have database locked) it mean that on the Adapter we can't pass only the path, we have to pass the full database object as it has to be a singleton. With the current implementation only one bean will work and all others will have lock issue

jaumard avatar Jul 06 '18 06:07 jaumard

Made a PR with my fix to have it working on my side https://github.com/jaguar-orm/sqflite/pull/1

jaumard avatar Jul 06 '18 06:07 jaumard

@jaumard Agree, we have to pass the full database path.

Same adapter can be shared with all beans, if it is the same database.

tejainece avatar Jul 06 '18 07:07 tejainece

I tried a simple one to one relation but it doesn't seems to work :(

Here is the generated insert method:

Future<Null> insert(Post model, {bool cascade: false}) async {
    final Insert insert = inserter.setMany(toSetColumns(model));
    await execInsert(insert);
    if (cascade) {
      Post newModel;
      if (model.item != null) {
        newModel ??= await find(model.id);
        itemBean.associatePost(model.item, newModel);
        await itemBean.insert(model.item);
      }
    }
  }

Why returning null now and not the id ? For me the correct generated method should be:

Future<dynamic> insert(Post model, {bool cascade: false}) async {
    final Insert insert = inserter.setMany(toSetColumns(model));
    var id = await execInsert(insert);
    if (cascade) {
      Post newModel;
      if (model.item != null) {
        newModel ??= await find(id);
        itemBean.associatePost(model.item, newModel);
        await itemBean.insert(model.item);
      }
    }
    return id;
  }

Because in case of auto increment id, the model.id will be null on the current behavior.

Also auto increment is not working, apparently some mistake in the sql query. I'll try to check why but I'll put all the problem I have here to not forget something

jaumard avatar Jul 06 '18 08:07 jaumard

@jaumard Did you tag the primary key field with @PrimaryKey() annotation. It will return the new id, if you tag the field.

tejainece avatar Jul 06 '18 08:07 tejainece

@tejainece yes I use your repo example and you put the @PrimaryKey() annotation on it

jaumard avatar Jul 06 '18 08:07 jaumard

If you want to check I put everything here https://github.com/jaumard/sqflite/tree/bugfix/fix_example

jaumard avatar Jul 06 '18 09:07 jaumard

Fixing.

tejainece avatar Jul 06 '18 09:07 tejainece

Could you send a pull request of your changes?

tejainece avatar Jul 06 '18 09:07 tejainece