Expression
is something that will evaluate to an int
when run on the database. If you add it to a select
, you can read the int value from a row. There's an example here.
Hello, I’m trying to migrate from Jaguar ORM to Moor, I’ve got a problem with the table definition because the create table query is not working :(
I’m getting SqliteException(1): near "id": syntax error
where the query is:
CREATE TABLE IF NOT EXISTS kiwi_transactions (id INTEGER NULL, ext_id INTEGER NOT NULL, amount REAL NOT NULL, amount_outstanding REAL NOT NULL, amount_reversed REAL NOT NULL, amount_change REAL NOT NULL, cash_received REAL NULL, currency VARCHAR NOT NULL, type INTEGER NOT NULL, modified INTEGER NOT NULL, added INTEGER NOT NULL, refunded INTEGER NOT NULL CHECK (refunded in (0, 1)), uuid VARCHAR NOT NULL, ext_tx_id VARCHAR NULL, longitude REAL NULL, latitude REAL NULL, is_cancelled INTEGER NOT NULL CHECK (is_cancelled in (0, 1)), is_reversed INTEGER NOT NULL CHECK (is_reversed in (0, 1)), result INTEGER NOT NULL, msi_enabled INTEGER NOT NULL CHECK (msi_enabled in (0, 1)), msi_merchant_id VARCHAR NULL, msi_description VARCHAR NULL, msi_currency VARCHAR NULL, msi_amount INTEGER NULL, msi_duration INTEGER NULL, msi_commission REAL NULL, cart_id INTEGER NULLABLE REFERENCE carts(id), cart_ext_id INTEGER NULLABLE REFERENCE carts(ext_id), events VARCHAR NOT NULL, PRIMARY KEY (ext_id));
So look like it doesn’t like the carts(id)
reference, but what is wrong with it ?
ALTER TABLE old_table RENAME TO new_table
.
import 'dart:convert';
import 'package:moor_flutter/moor_flutter.dart';
import 'package:json_annotation/json_annotation.dart' as j;
part 'court_type.g.dart';
@j.JsonSerializable()
class DataModel {
int id;
String name;
String uniqueUuid;
String languages;
String description;
String timer;
String importantDates;
String createdAt;
String updatedAt;
DataModel({
this.id,
this.name,
this.uniqueUuid,
this.languages,
this.description,
this.timer,
this.importantDates,
this.createdAt,
this.updatedAt});
factory DataModel.fromJson(Map<String, dynamic> json) =>
_$DataModelFromJson(json);
Map<String, dynamic> toJson() => _$DataModelToJson(this);}
class DataModelConverter extends TypeConverter<DataModel, String> {
const DataModelConverter();
@override
DataModel mapToDart(String fromDb) {
if (fromDb == null) {
return null;
}
return DataModel.fromJson(json.decode(fromDb) as Map<String, dynamic>);
}
@override
String mapToSql(DataModel value) {
if (value == null) {
return null;
}
return json.encode(value.toJson());
}
}
am i doing anything wrong in this code? my court_type.g.dart file is not generating
That code looks alright to me. Moor isn't supposed to generate
court_type.g.dart
, thejson_serializable
package should do that. Did you add adev_dependency
on that pacakge?
thanks man it worked! can you explain something to me? that JsonSerializable was importing fine even when i didn't putted it in dev deps. But now when i putted it after you told me it generated the file? can you explain me what exactly happed?
hi, i am trying to access typeconverter's fields and its getting null
here's my class
import 'dart:convert';
import 'package:moor_flutter/moor_flutter.dart';
import 'package:json_annotation/json_annotation.dart';
part 'courtType.typeconverter.g.dart';
@JsonSerializable()
class CourtTypeColumn {
CourtTypeColumn();
int _id;
String _name;
int get id => _id;
String get name => _name;
factory CourtTypeColumn.fromJson(Map<String, dynamic> json) => _$CourtTypeColumnFromJson(json);
Map<String, dynamic> toJson() => _$CourtTypeColumnToJson(this);
}
class CourtTypeConverter extends TypeConverter<CourtTypeColumn, String> {
const CourtTypeConverter();
@override
CourtTypeColumn mapToDart(String fromDb) {
if (fromDb == null) {
return null;
}
return CourtTypeColumn.fromJson(json.decode(fromDb) as Map<String, dynamic>);
}
@override
String mapToSql(CourtTypeColumn value) {
if (value == null) {
return null;
}
return json.encode(value.toJson());
}
}
and also m pretty sure that data is getting saved because the field is not null in my table and its not even throwing any error while saving data to table
but when im trying to access it after getting it the data through my dao its getting null
TextColumn get courtType => text().map(const CourtTypeConverter())();
Future<TbUserData> getUser() {
return (select(tbUser)..where((tbl) => tbl.status.equals(true))).getSingle();
}
TbUserData user = await _database.userDao.getUser();
print(user. courtType.name); // which is printing null
yea
If
user.courtType.name
prints null (instead of throwing a NoSuchMethodError aboutname
not existing onnull
), it looks like the type converter was applied? Can you check thatCourtTypeColumn.fromJson
works as expected?
yea i already checked it it returns an empty map