angel
angel copied to clipboard
ORM relation hasMany don't generate
Hey, I didn't find any examples of complex relation between tables except the doc. I have an existing database where I want to plug a Dart server. I've tried this:
import 'package:angel3_orm/angel3_orm.dart';
import 'package:angel3_serialize/angel3_serialize.dart';
part 'shop.g.dart';
@Orm(tableName: 'contracts_shop', generateMigrations: false)
@serializable
abstract class _Shop {
/// A unique identifier corresponding to this item.
@Column(indexType: IndexType.primaryKey)
int? id;
String get name;
int? get industryId;
DateTime get added;
@HasMany(foreignKey: 'mx_cashadvancepropos_shop_id_5f99e2ca_fk_contracts')
List<_Supplier> get suppliers;
}
@Orm(tableName: 'suppliers_supplier', generateMigrations: false)
@serializable
abstract class _Supplier {
/// A unique identifier corresponding to this item.
@Column(indexType: IndexType.primaryKey)
int? id;
@BelongsTo(foreignKey: 'mx_cashadvancepropos_shop_id_5f99e2ca_fk_contracts')
_Shop get shop;
String get fullName;
String get email;
String get iban;
String get accountName;
String? get phoneNumber;
@Column(defaultValue: true)
bool get isEnabled;
DateTime get lastMod;
DateTime get added;
}
But the generation command gives:
_Shop has no field that maps to the name "mx_cashadvancepropos_shop_id_5f99e2ca_fk_contracts", but _Supplier has a @HasMany() relation that expects such a field.
I've make some adjustments on the two classes.
A couple of notes:
- Turning on migration can make debugging easier since can see what are the structure of the generated tables. They are not automatically executed anyway.
- HasMany -> foreignKey refers to Foreign Table primary key or any valid field
- BelongsTo -> foreignKey refers to Foreign Table primary key or any valid field
- Shop should be nullable type. This is a limitation as non-nullable Shop would need a default Shop to be assigned in order to remain non-nullable. There is no easy way to do this. This is one area where reflection would be very helpful.
import 'package:angel3_migration/angel3_migration.dart';
import 'package:angel3_orm/angel3_orm.dart';
import 'package:angel3_serialize/angel3_serialize.dart';
import 'package:optional/optional.dart';
part 'shop.g.dart';
@Orm(tableName: 'contracts_shop')
@serializable
abstract class _Shop {
/// A unique identifier corresponding to this item.
@Column(indexType: IndexType.primaryKey)
int? id;
String get name;
int? get industryId;
DateTime get added;
@HasMany(foreignKey: 'id')
List<_Supplier> get suppliers;
}
@Orm(tableName: 'suppliers_supplier')
@serializable
abstract class _Supplier {
/// A unique identifier corresponding to this item.
@Column(indexType: IndexType.primaryKey)
int? id;
@BelongsTo(foreignKey: 'id')
_Shop? get shop;
String get fullName;
String get email;
String get iban;
String get accountName;
String? get phoneNumber;
@Column(defaultValue: true)
bool get isEnabled;
DateTime get lastMod;
DateTime get added;
}