Closed jasteeman closed 4 years ago
but in the repository on github I could not perform either
What do you mean? The example does not work for you?
You code looks ok but I would need more info, at least a line number in the stack trace, at best some code to reproduce the bug: I can not reproduce it now
Hi @synw I already solved my problem, I had an error in the relation of the foreign key,my mistake was that in the class I defined product and in the table I put products..
Hi @synw . I'm having trouble bringing in a serialized model with a relationship from another model class,below I attach my case..
MY CLASS MODEL IS:
class ProductoItem with DbModel {
// Propiedades
String unidadMedida; double cantidad; double precioUnitario; double descuento; ProductoModel producto; Pedido pedido;
ProductoItem( {this.id, this.unidadMedida, this.cantidad, this.precioUnitario, this.descuento, this.producto, this.pedido});
/// [DbModel] required overrides /// @override int id;
@override Db get db => conf.db;
DbTable get table => productoPedidoTable;
/// serialize a row to the database @override Map<String, dynamic> toDb() { // we want the foreign key to be recorded assert(pedido?.id != null); assert(producto?.id!=null); final row = <String, dynamic>{ 'unidadMedida': unidadMedida, 'cantidad': cantidad, 'precioUnitario': precioUnitario, 'descuento': descuento, 'producto': producto.id, "pedido": pedido.id }; return row; }
/// deserialize a row from database @override ProductoItem fromDb(Map<String, dynamic> map) { final producto = ProductoItem( id: map["id"] as int, unidadMedida: map["unidadMedida"].toString(), cantidad: map["cantidad"] as double, precioUnitario: map["precioUnitario"] as double, descuento: map["descuento"] as double, ); // the key will be present only with join queries // in a simple select this data is not present if (map.containsKey("pedido")) { producto.pedido = Pedido().fromDb(map["pedido"] as Map<String, dynamic>); }
}
/// Create a static join method for convenience
static Future<List> selectRelated({String where, int limit}) async {
final cars = List.from(
await ProductoItem().sqlJoin(where: where, limit: limit));
return cars;
}
}
when using this query to see the attributes of the product, I get an exception
final c = await ProductoItem.selectRelated(); print("Found ${c[0].producto.nombre} cars: $c");
Exception has occurred. NoSuchMethodError (NoSuchMethodError: The getter 'name' was called on null. Receiver: null Tried calling: name)
I don't know why throws that exception but in the repository on github I could not perform either, but in theory it should work according to the doc ( https://pub.dev/packages/sqlcool)