Open jaumard opened 4 years ago
My models
import 'package:jaguar_orm/jaguar_orm.dart';
import 'package:kiwi_mobile/cart/models/cart_item.dart';
import 'package:meta/meta.dart';
part 'cart.jorm.dart';
@immutable
class Cart {
static const int currentCartId = 1;
@Column(isNullable: true)
final int id;
@PrimaryKey(isNullable: false)
final int extId;
@HasMany(CartItemBean)
List<CartItem> items;
@Column()
final double amount;
@Column()
final int modified;
Cart({@required this.extId, this.amount = 0.0, this.id, this.modified, this.items = const <CartItem>[]});
}
@GenBean()
class CartBean extends Bean<Cart> with _CartBean {
CartBean(Adapter adapter) : super(adapter);
CartItemBean _cartItemBean;
@override
String get tableName => 'cart';
@override
CartItemBean get cartItemBean => _cartItemBean ??= CartItemBean(adapter);
}
And:
import 'dart:async';
import 'package:jaguar_orm/jaguar_orm.dart';
import 'package:kiwi_mobile/cart/models/cart.dart';
import 'package:meta/meta.dart';
part 'cart_item.jorm.dart';
@immutable
class CartItem {
@PrimaryKey(auto: true)
final int id;
@Column()
final double amount;
@Column(isNullable: true)
final int product;
@Column()
final int quantity;
@Column(isNullable: true)
final String description;
@BelongsTo(CartBean, refCol: 'extId', byHasMany: true)
int cartId;
CartItem({@required this.amount, this.quantity = 1, this.description, this.product, this.id, this.cartId});
CartItem.make(this.amount, this.description, this.product, this.quantity, {this.cartId, this.id});
}
@GenBean()
class CartItemBean extends Bean<CartItem> with _CartItemBean {
CartItemBean(Adapter adapter) : super(adapter);
CartBean _cartBean;
@override
String get tableName => 'cartItem';
@override
CartBean get cartBean => _cartBean ??= CartBean(adapter);
}
With SQFlite I notice that create table generate this:
As you can see the foreign key on cartItem table is wrong as it reference cart(extId) instead of cart(ext_id).
So all foreign key are wrong using sqflite adapter :/
By "chance" sqlite doesn't enable this constraint by default, but once you enable it, it's Christmas on errors...