Foreign keys wrongly generated
jaumard opened this issue · 1 comments
jaumard commented
With SQFlite I notice that create table generate this:
CREATE TABLE IF NOT EXISTS cart (id INT, ext_id INT NOT NULL, amount REAL NOT NULL, modified INT NOT NULL, transaction_id INT, PRIMARY KEY (ext_id))
CREATE TABLE IF NOT EXISTS cartItem (id INTEGER PRIMARY KEY NOT NULL, amount REAL NOT NULL, product INT, quantity INT NOT NULL, description CHAR(20), cart_id INT NOT NULL, FOREIGN KEY (cart_id) REFERENCES cart(extId))
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...
jaumard commented
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);
}