dart-backend / angel

A polished, production-ready backend framework in Dart for the VM, AOT, and Flutter.
https://github.com/dukefirehawk/angel
BSD 3-Clause "New" or "Revised" License
171 stars 22 forks source link

How to override id type? #91

Open kevinelliott opened 1 year ago

kevinelliott commented 1 year ago

For ORM model, how do you override the id type to be UUID (String)? There is some stuff that forces id as int for query, and this is problematic.

dukefirehawk commented 1 year ago

I think this could be a bug by looking at the generated code when id is String. The primary key is assumed to be auto generated numeric field in the where clause and table creation. Will issue a fix for this.

kevinelliott commented 1 year ago

Great, thanks @dukefirehawk !

dukefirehawk commented 1 year ago

@kevinelliott Have you tried the following annotation?

@PrimaryKey(columnType: ColumnType.varChar)
String? get id;

Take a look at email_indexed.dart in the angel_orm_test package. Let us know if this works.

kevinelliott commented 1 year ago

@dukefirehawk Yes, when I try that, I get:

lib/marine/orm/managers/voyage_manager.dart:60:33: Error: Can't use 'id' because it is declared more than once.
      ..where!.id.equals(voyage.id!)
                                ^^
lib/marine/orm/managers/voyage_manager.dart:60:33: Error: The getter 'id' isn't defined for the class 'Voyage'.
 - 'Voyage' is from 'package:aggregation_server/marine/orm/models/voyage.dart' ('lib/marine/orm/models/voyage.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'id'.
      ..where!.id.equals(voyage.id!)
                                ^^
lib/marine/orm/managers/voyage_position_manager.dart:39:27: Error: Can't use 'id' because it is declared more than once.
      ..voyageId = voyage.id
                          ^^
lib/marine/orm/managers/voyage_position_manager.dart:39:27: Error: The getter 'id' isn't defined for the class 'Voyage'.
 - 'Voyage' is from 'package:aggregation_server/marine/orm/models/voyage.dart' ('lib/marine/orm/models/voyage.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'id'.
      ..voyageId = voyage.id
                          ^^
lib/marine/orm/models/voyage.g.dart:322:12: Error: Duplicated parameter name 'id'.
      this.id,
           ^^
lib/marine/orm/models/voyage.g.dart:319:13: Context: Other parameter named 'id'.
      {this.id,
            ^^
lib/marine/orm/models/voyage.g.dart:349:11: Error: 'id' is already declared in this scope.
  String? id;
          ^^
lib/marine/orm/models/voyage.g.dart:338:11: Context: Previous declaration of 'id'.
  String? id;
          ^^
lib/marine/orm/models/voyage.g.dart:391:15: Error: Duplicated parameter name 'id'.
      String? id,
              ^^
lib/marine/orm/models/voyage.g.dart:388:16: Context: Other parameter named 'id'.
      {String? id,
               ^^
lib/marine/orm/models/voyage.g.dart:608:23: Error: 'id' is already declared in this scope.
  static const String id = 'id';
                      ^^
lib/marine/orm/models/voyage.g.dart:602:23: Context: Previous declaration of 'id'.
  static const String id = 'id';
                      ^^
lib/marine/orm/models/voyage.g.dart:587:5: Error: Can't use 'id' because it is declared more than once.
    id,
    ^
lib/marine/orm/models/voyage.g.dart:584:5: Error: Can't use 'id' because it is declared more than once.
    id,
    ^
lib/marine/orm/models/voyage.g.dart:319:13: Error: Can't use 'id' because it is declared more than once.
      {this.id,
            ^^
lib/marine/orm/models/voyage.g.dart:322:12: Error: Can't use 'id' because it is declared more than once.
      this.id,
           ^^
lib/marine/orm/models/voyage.g.dart:405:24: Error: Can't use 'id' because it is declared more than once.
        id: id ?? this.id,
                       ^^
lib/marine/orm/models/voyage.g.dart:405:24: Error: The getter 'id' isn't defined for the class 'Voyage'.
 - 'Voyage' is from 'package:aggregation_server/marine/orm/models/voyage.dart' ('lib/marine/orm/models/voyage.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'id'.
        id: id ?? this.id,
                       ^^
lib/marine/orm/models/voyage.g.dart:408:24: Error: Can't use 'id' because it is declared more than once.
        id: id ?? this.id,
                       ^^
lib/marine/orm/models/voyage.g.dart:408:24: Error: The getter 'id' isn't defined for the class 'Voyage'.
 - 'Voyage' is from 'package:aggregation_server/marine/orm/models/voyage.dart' ('lib/marine/orm/models/voyage.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'id'.
        id: id ?? this.id,
                       ^^
lib/marine/orm/models/voyage.g.dart:408:9: Error: Duplicated named argument 'id'.
        id: id ?? this.id,
        ^^
lib/marine/orm/models/voyage.g.dart:430:21: Error: Can't use 'id' because it is declared more than once.
        other.id == id &&
                    ^
lib/marine/orm/models/voyage.g.dart:433:21: Error: Can't use 'id' because it is declared more than once.
        other.id == id &&
                    ^
lib/marine/orm/models/voyage.g.dart:454:7: Error: Can't use 'id' because it is declared more than once.
      id,
      ^
lib/marine/orm/models/voyage.g.dart:451:7: Error: Can't use 'id' because it is declared more than once.
      id,
      ^
lib/marine/orm/models/voyage.g.dart:472:24: Error: Can't use 'id' because it is declared more than once.
    return 'Voyage(id=$id, createdAt=$createdAt, updatedAt=$updatedAt, id=$id, vesselId=$vesselId, departurePortScheduledId=$departurePortScheduledId, departurePortActualId=$departurePortActualId, departureTimeScheduled=$departureTimeScheduled, departureTimeActual=$departureTimeActual, arrivalPortScheduledId=$arrivalPortScheduledId, arrivalPortActualId=$arrivalPortActualId, arrivalTimeScheduled=$arrivalTimeScheduled, arrivalTimeActual=$arrivalTimeActual, latestLatitude=$latestLatitude, latestLongitude=$latestLongitude, status=$status)';
                       ^
lib/marine/orm/models/voyage.g.dart:472:76: Error: Can't use 'id' because it is declared more than once.
    return 'Voyage(id=$id, createdAt=$createdAt, updatedAt=$updatedAt, id=$id, vesselId=$vesselId, departurePortScheduledId=$departurePortScheduledId, departurePortActualId=$departurePortActualId, departureTimeScheduled=$departureTimeScheduled, departureTimeActual=$departureTimeActual, arrivalPortScheduledId=$arrivalPortScheduledId, arrivalPortActualId=$arrivalPortActualId, arrivalTimeScheduled=$arrivalTimeScheduled, arrivalTimeActual=$arrivalTimeActual, latestLatitude=$latestLatitude, latestLongitude=$latestLongitude, status=$status)';
                                                                           ^
lib/marine/orm/models/voyage.g.dart:525:9: Error: Duplicated named argument 'id'.
        id: map['id'] as String?,
        ^^
Error: AOT compilation failed
Generating AOT kernel dill failed!
kevinelliott commented 1 year ago

@dukefirehawk Any thoughts?

kevinelliott commented 1 year ago

OK, so turns out it was just because I was extending Model. If I remove that, it all seemingly works (thus no collision there). Hopefully I'm not missing out on anything else by not extending!

dukefirehawk commented 1 year ago

We are currently looking into updating ORM to support models defined in multiple files. Will look into this issue together.

alguintu commented 6 months ago

@dukefirehawk

We are currently looking into updating ORM to support models defined in multiple files. Will look into this issue together.

Any update to this feature? Found out this was the missing info to why my attempts to create relationships was failing. Models NEEDED to be in a single file. This info should be prominent in the documentation. I was wondering how the examples were working importing private classes like:

@hasMany
List<_Book> get books;

@belongsTo
_Author get author;

Took me way too long to figure this out. For now I started using part of to maintain the models in separate files, bridging them together with a generic models.dart and models.g.dart as a workaround. Hopefully this feature gets rolled out soon.