Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ORM relation hasMany don't generate #76

Open
jaumard opened this issue Aug 17, 2022 · 3 comments
Open

ORM relation hasMany don't generate #76

jaumard opened this issue Aug 17, 2022 · 3 comments
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@jaumard
Copy link

jaumard commented Aug 17, 2022

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.
@dukefirehawk dukefirehawk self-assigned this Aug 20, 2022
@dukefirehawk dukefirehawk added the bug Something isn't working label Aug 20, 2022
@dukefirehawk
Copy link
Collaborator

dukefirehawk commented Aug 20, 2022

I've make some adjustments on the two classes.

A couple of notes:

  1. Turning on migration can make debugging easier since can see what are the structure of the generated tables. They are not automatically executed anyway.
  2. HasMany -> foreignKey refers to Foreign Table primary key or any valid field
  3. BelongsTo -> foreignKey refers to Foreign Table primary key or any valid field
  4. 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;
}

@dukefirehawk dukefirehawk added documentation Improvements or additions to documentation and removed bug Something isn't working labels Aug 20, 2022
@alguintu
Copy link

alguintu commented Dec 27, 2023

Trying to get basic relationships to work and hitting a hard wall using the latest releases:

part 'franchise.g.dart';

@orm
@serializable
abstract class _Franchise extends Model {
  String get name;

  @hasMany
  List<Zone>? get zones;
  // List<_Zone> get zones; fails to import zone as well
}
part 'zone.g.dart';

@orm
@serializable
abstract class _Zone extends Model {
  @belongsTo
  Franchise? get franchise;

  String get name;

  @DefaultsTo(0.1)
  double get franchiseRate;

  @DefaultsTo(false)
  bool get isActive;

  @DefaultsTo(false)
  bool get isSuspended;
}

I can't figure out what I'm doing wrong if any, and the documentation does not have an example for @hasMany. The moment I introduce any relationship, either hasMany or belongsTo, the generator stops:

[INFO] ------------------------------------------------------------------------
[INFO] Starting Build
[INFO] Updating asset graph completed, took 1ms
[SEVERE] angel3_serialize_generator:angel_serialize on lib/src/models/franchise/franchise.dart:

Tried to construct class "TypeReference" with null for non-nullable field "symbol".
[SEVERE] angel3_serialize_generator:angel_serialize on lib/src/models/franchise/franchise.dart:

Null check operator used on a null value
[SEVERE] angel3_orm_generator:angel3_orm on lib/src/models/franchise/franchise.dart:

Tried to construct class "TypeReference" with null for non-nullable field "symbol".
[INFO] Running build completed, took 76ms
[INFO] Caching finalized dependency graph completed, took 22ms
[SEVERE] Failed after 101ms
[INFO] ------------------------------------------------------------------------
[WARNING] build_resolvers:transitive_digests on lib/src/models/franchise/franchise.dart:
Unable to read asset, could not compute transitive deps: halo|lib/src/models/franchise/franchise.g.dart

This may cause less efficient builds, see the following doc for help:
https://github.com/dart-lang/build/blob/master/docs/faq.md#unable-to-read-asset-could-not-compute-transitive-deps
[WARNING] build_resolvers:transitive_digests on bin/migrate.dart:
Unable to read asset, could not compute transitive deps: halo|lib/src/models/franchise/franchise.g.dart

This may cause less efficient builds, see the following doc for help:
https://github.com/dart-lang/build/blob/master/docs/faq.md#unable-to-read-asset-could-not-compute-transitive-deps
[INFO] Running build completed, took 95ms
[INFO] Caching finalized dependency graph completed, took 23ms
[SEVERE] Failed after 121ms

@dukefirehawk
Copy link
Collaborator

dukefirehawk commented Dec 30, 2023

At the moment, models with relationship need to be defined in the same file. This is a known limitation and pain point that will be addressed soon. The following revised code should work. Noted on the document, it will be updated.

import 'package:angel3_migration/angel3_migration.dart';
import 'package:angel3_serialize/angel3_serialize.dart';
import 'package:angel3_orm/angel3_orm.dart';
import 'package:optional/optional.dart';

part 'franchise.g.dart';

@orm
@serializable
abstract class _Zone extends Model {
  @belongsTo
  _Franchise? get franchise;

  String get name;

  @DefaultsTo(0.1)
  double get franchiseRate;

  @DefaultsTo(false)
  bool get isActive;

  @DefaultsTo(false)
  bool get isSuspended;
}

@orm
@serializable
abstract class _Franchise extends Model {
  String get name;

  @hasMany
  List<_Zone> get zones;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants