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

I am not getting error if id field is not auto and has no value in Flutter. #129

Closed ziakhan110 closed 5 years ago

ziakhan110 commented 5 years ago

i have a model named Account which has id field which is not auto incremented. i was inserting it without providing value for id field. i thought i had set it to auto but i didn't. The app was not loading just black screen and not errors or exception in console.

I would really like to get an error or some exception in console to know what is wrong.

JamesMcIntosh commented 5 years ago

@ziakhan110 You should provide an example of code, if you annotated the field with @PrimaryKey then the column should have a "not null" constraint on the column.

ziakhan110 commented 5 years ago

this is my model

class Account {
  Account();

  Account.make(this.name, this.startBalance, this.dateCreated);

  @PrimaryKey
  int id;
  @Column(isNullable: false)
  String name;
  @Column(isNullable: true)
  double startBalance;
  @Column(isNullable: false)
  String dateCreated;
  @HasMany(TransactionsBean)
  List<Transactions> transactions;
}

this my bean .

@GenBean()
class AccountBean extends Bean<Account> with _AccountBean {
  TransactionsBean transactionsBean;

  AccountBean(Adapter adapter)
      : transactionsBean = TransactionsBean(adapter),
        super(adapter);

  final String tableName = 'account';
}

this insertion code

var personal = Account.make(
            "Personal", 0.0, DateFormat.yMMMd().format(DateTime.now()));

        await accountBean.createTable(ifNotExists: true);
        await accountBean.insert(personal);
JamesMcIntosh commented 5 years ago

@ziakhan110 What does the createTable function inside of the generated AccountBean.jorm.dart file look like?

ziakhan110 commented 5 years ago

Here is createTable

Future<void> createTable({bool ifNotExists = false}) async {
    final st = Sql.create(tableName, ifNotExists: ifNotExists);
    st.addInt(id.name, primary: true, isNullable: false);
    st.addStr(name.name, isNullable: false);
    st.addDouble(startBalance.name, isNullable: true);
    st.addStr(dateCreated.name, isNullable: false);
    return adapter.createTable(st);
  }
JamesMcIntosh commented 5 years ago

@ziakhan110 I get the following in the log when I try and run the insert

SQLiteLog: (1299) abort at 8 in [INSERT INTO account(id, name, start_balance, date_created) VALUES (null, 'Personal', 0.0, 'Jun 15, 2019')]: NOT NULL constraint failed: account.id
flutter: error DatabaseException(NOT NULL constraint failed: account.id (code 1299)) sql 'INSERT INTO account(id, name, start_balance, date_created) VALUES (null, 'Personal', 0.0, 'Jun 15, 2019')' args []} during open, closing...
I/flutter: DatabaseException(NOT NULL constraint failed: account.id (code 1299)) sql 'INSERT INTO account(id, name, start_balance, date_created) VALUES (null, 'Personal', 0.0, 'Jun 15, 2019')' args []}
I/flutter: #0      wrapDatabaseException (package:sqflite/src/exception_impl.dart:11:7)
    <asynchronous suspension>
    #1      SqfliteDatabaseFactoryImpl.wrapDatabaseException (package:sqflite/src/factory_impl.dart:29:7)
    #2      _SqfliteDatabaseBase&Object&SqfliteDatabaseMixin.safeInvokeMethod (package:sqflite/src/database_mixin.dart:183:15)
    #3      _SqfliteDatabaseBase&Object&SqfliteDatabaseMixin.txnRawInsert.<anonymous closure> (package:sqflite/src/database_mixin.dart:340:14)
    #4      _SqfliteDatabaseBase&Object&SqfliteDatabaseMixin.txnSynchronized (package:sqflite/src/database_mixin.dart:275:26)
    <asynchronous suspension>
    #5      _SqfliteDatabaseBase&Object&SqfliteDatabaseMixin.txnWriteSynchronized (package:sqflite/src/database_mixin.dart:307:7)
    #6      _SqfliteDatabaseBase&Object&SqfliteDatabaseMixin.txnRawInsert (package:sqflite/src/database_mixin.dart:339:12)
    #7      _SqfliteDatabaseBase&Object&SqfliteDatabaseMixin&SqfliteDatabaseExecutorMixin.rawInsert (package:sqflite/src/database_mixin.dart:44:15)
    #8      SqfliteAdap
06-15 11:31:18.709 20813-20830/app.test.com.testapp I/flutter: Transition { currentState: Instance of 'InitialDataListState', event: Fetch, nextState: DataListError }
ziakhan110 commented 5 years ago

@JamesMcIntosh this is what i get , shown in picture flutter flutter 2

ziakhan110 commented 5 years ago

i am inserting data at app startup, maybe it has something to do with it, when i try it when app is loaded then it gives me error.

tejainece commented 5 years ago

@ziakhan110 If i understand this right, the insert fails because you are trying to insert with null id, which is a non nullable primary key.

You have three options:

1) Set id manually (Because by default @PrimaryKey makes the column "NOT NULL") 2) Use some kind of sequence column type 3) Manually set that the column can have null values (@PrimaryKey(isNullable: true))

ziakhan110 commented 5 years ago

i know my options i have set it to auto=true. what i am asking is why i am not getting error in console when there is no value provided.

tejainece commented 5 years ago

@jaumard can you see why an exception is not thrown here?

ziakhan110 commented 5 years ago

@tejainece maybe exception is thrown but flutter log is not initialized yet. https://stackoverflow.com/questions/55393207/how-to-catch-flutter-app-crash-on-startup

tejainece commented 5 years ago

Thanks for the info. Should we close the issue then?

ziakhan110 commented 5 years ago

@tejainece yes sir.