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

Non-orm serializable jsonb column, can't run migration. #118

Open
alguintu opened this issue Dec 31, 2023 · 1 comment
Open

Non-orm serializable jsonb column, can't run migration. #118

alguintu opened this issue Dec 31, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@alguintu
Copy link

alguintu commented Dec 31, 2023

When using a non-orm serializable as a column, source_gen triggers a warning that leads to migration fail:

// simplified example
@orm
@serializable
abstract class _User extends Model {
  String get name;
  _Profile? get profile;
}

@serializable
abstract class _Profile {
  String get firstName;
  String get lastName;
}

Running build_runner succeeds with the following warning:

[INFO] ------------------------------------------------------------------------
[INFO] Starting Build
[INFO] Updating asset graph completed, took 2ms
[WARNING] angel3_orm_generator:angel3_orm on lib/src/models/models.dart:
Cannot generate ORM code for field profile of type _Profile?
[INFO] Running build completed, took 1.8s
[INFO] Caching finalized dependency graph completed, took 40ms
[INFO] Succeeded after 1.9s with 2 outputs (2 actions)

And then when I try to run dart bin/migrate.dart up I get the following error:

../core/lib/src/models/models.g.dart:1020:7: Error: The getter 'profile' isn't defined for the class 'UserQueryWhere'.
 - 'UserQueryWhere' is from 'package:core/src/models/models.dart' ('../core/lib/src/models/models.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'profile'.
      profile,
      ^^^^^^^

What am I missing here? The point is to use a serializable class for a jsonb column, but the generator seems to want to be able to query using that jsonb field. Any hints to what I'm doing wrong would be really appreciated. Thanks.

@dukefirehawk
Copy link
Collaborator

The jsonb data type can be done as follows. The @Column annotation is optional. Just added it for clarity. @serializable annotation is not designed to work with jsonb object. Check out the test case has_map_test.dart under angel_orm_test folder.

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

  @Column(type: ColumnType.jsonb)
  Map<String, String>? profile = {};

  void updateProfile(Profile p) {
    profile = p.toMap();
  }

  Profile retrieveProfile() {
    return Profile()..fromMap(profile ?? {});
  }
}

class Profile {
  String firstName = '';
  String lastName = '';

  Map<String, String> toMap() {
    var result = <String, String>{};
    result['firstName'] = firstName;
    result['lastName'] = lastName;
    return result;
  }

  void fromMap(Map<String, String> m) {
    firstName = m['firstName'] ?? '';
    lastName = m['lastName'] ?? '';
  }
}

@dukefirehawk dukefirehawk added the enhancement New feature or request label Jul 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants