Jaguar-dart / jaguar_orm

Source-generated ORM with relations (one-to-one, one-to-many, many-to-many), preloading, cascading, polymorphic relations, etc
https://jaguar-dart.github.io
BSD 3-Clause "New" or "Revised" License
217 stars 52 forks source link

Add tests to make sure sqflite composer is solid #23

Open tejainece opened 6 years ago

tejainece commented 6 years ago
tejainece commented 6 years ago

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

jaumard commented 6 years ago

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

tejainece commented 6 years ago

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

Try the example project.

jaumard commented 6 years ago

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

jaumard commented 6 years ago

@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 commented 6 years ago

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

tejainece commented 6 years ago

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

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

jaumard commented 6 years ago

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

tejainece commented 6 years ago

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

jaumard commented 6 years ago

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

jaumard commented 6 years ago

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

tejainece commented 6 years ago

Fixing.

tejainece commented 6 years ago

Could you send a pull request of your changes?