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

Relations are throwing error when you use include. #324

Closed
kidusdev opened this issue Jan 18, 2024 · 7 comments
Closed

Relations are throwing error when you use include. #324

kidusdev opened this issue Jan 18, 2024 · 7 comments
Assignees
Labels
bug Something isn't working

Comments

@kidusdev
Copy link

kidusdev commented Jan 18, 2024

Relations are not correctly converted to lists when finding user and its many posts.

// ./prisma/schema.prisma

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
  provider = "dart run orm"
  output   = "../lib/prisma"
}

datasource db {
  provider = "sqlite"
  url      = env("DATABASE_URL")
}

model users {
  id    Int     @id @default(autoincrement())
  name  String?
  email String
  posts posts[]
}

model posts {
  id      Int    @id @default(autoincrement())
  title   String
  author  users  @relation(fields: [usersId], references: [id])
  usersId Int
}
//./lib/index.dart
final prisma = PrismaClient(datasourceUrl: "file:./prisma/dev.db");

void run() async {
  final users = await prisma.users.findMany(include: UsersInclude(posts: PrismaUnion.$1(true)));

  for (var user in users) {
    print("");
    print(user.name);

    for (var post in user.posts!) {
      print("    ${post.title!}");
    }
  }
}

// error
Unhandled exception:
type 'MappedListIterable<dynamic, dynamic>' is not a subtype of type 'Iterable<Map<dynamic, dynamic>>?' in type cast
#0      new Users.fromJson (package:test/prisma/model.dart:44:31)
#1      UsersDelegate.findMany.<anonymous closure>.<anonymous closure> (package:test/prisma/client.dart:188:53)
#2      MappedListIterable.elementAt (dart:_internal/iterable.dart:425:31)
#3      ListIterator.moveNext (dart:_internal/iterable.dart:354:26)
#4      run (package:test/test.dart:12:20)
<asynchronous suspension>
name: test
description: A sample command-line application.
version: 1.0.0
# repository: https://github.com/my_org/my_repo

environment:
  sdk: ^3.2.3

# Add regular dependencies here.
dependencies:
  orm: 4.0.0-beta

dev_dependencies:
  lints: ^2.1.0

When i go to the library's source code i found the issues on the generated model file

// ./lib/prisma/model.dart


class Users {
  const Users({
    this.id,
    this.name,
    this.email,
    this.posts,
    this.$count,
  });

  factory Users.fromJson(Map json) => Users(
        id: json['id'],
        name: json['name'],
        email: json['email'],
         // changed this
        posts: (json['posts'] as Iterable<Map>?)?.map(_i1.Posts.fromJson),
         // into this and it worked
        // posts: (List<Map>.from(json['posts'])).map(_i1.Posts.fromJson),
        $count: json['_count'] is Map ? _i2.UsersCountOutputType.fromJson(json['_count']) : null,
      );

  final int? id;

  final String? name;

  final String? email;

  final Iterable<_i1.Posts>? posts;

  final _i2.UsersCountOutputType? $count;
}

@medz medz self-assigned this Jan 18, 2024
@medz medz added bug Something isn't working dart labels Jan 18, 2024
@medz
Copy link
Owner

medz commented Jan 18, 2024

@kidusdev Please provide the version number of orm

@medz
Copy link
Owner

medz commented Jan 18, 2024

Thx, I will verify and fix it as soon as possible.

@kidusdev
Copy link
Author

kidusdev commented Jan 18, 2024

@medz it is in the generated user model check at the bottom of the issue code

@medz
Copy link
Owner

medz commented Jan 18, 2024

@medz medz closed this as completed Jan 18, 2024
@kidusdev
Copy link
Author

@medz wow that was fast. 👏👏👏 it is solved.

@medz
Copy link
Owner

medz commented Jan 18, 2024

I should thank you for providing such accurate information so that I can quickly fix it and create test code for it.

@ebwood
Copy link

ebwood commented Feb 6, 2024

If the type in .prisma is primitive list type like Int[], then the generated fromJson will cause error:
type 'MappedListIterable<dynamic, dynamic>' is not a subtype of type 'Iterable<int>?'.
It should change from like json['posts'] to List.from(json['posts']

model User {
	posts Int[]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants