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

drift file view "Expression has an unknown type, the generated code can be inaccurate." #2844

Closed
greenking19 opened this issue Jan 18, 2024 · 2 comments

Comments

@greenking19
Copy link

I created a view using a drive file, and the build process always prompts
"Expression has an unknown type, the generated code can be inaccurate."
How to solve this problem?
thanks!

task table

CREATE TABLE tasks (
    id INT NOT NULL PRIMARY KEY AUTOINCREMENT,
    title TEXT NOT NULL
);

todo table

CREATE TABLE todo (
    id INT NOT NULL PRIMARY KEY AUTOINCREMENT,
    task_id TEXT NOT NULL,
    content TEXT NOT NULL
);

tabk view

CREATE VIEW task_view AS
SELECT 
    t.id as id, 
    td.content as todo_content
FROM tasks t
INNER JOIN todo td ON t.id = td.task_id;

database.g.dart TaskViewData

It is obvious that the id field type should be int to be correct

class TaskViewData extends DataClass {
  final String? id;
  final String? todoContent;
  const TaskViewData({this.id, this.todoContent});
  factory TaskViewData.fromJson(Map<String, dynamic> json,
      {ValueSerializer? serializer}) {
    serializer ??= driftRuntimeOptions.defaultSerializer;
    return TaskViewData(
      id: serializer.fromJson<String?>(json['id']),
      todoContent: serializer.fromJson<String?>(json['todo_content']),
    );
  }
  @override
  Map<String, dynamic> toJson({ValueSerializer? serializer}) {
    serializer ??= driftRuntimeOptions.defaultSerializer;
    return <String, dynamic>{
      'id': serializer.toJson<String?>(id),
      'todo_content': serializer.toJson<String?>(todoContent),
    };
  }

  TaskViewData copyWith(
          {Value<String?> id = const Value.absent(),
          Value<String?> todoContent = const Value.absent()}) =>
      TaskViewData(
        id: id.present ? id.value : this.id,
        todoContent: todoContent.present ? todoContent.value : this.todoContent,
      );
  @override
  String toString() {
    return (StringBuffer('TaskViewData(')
          ..write('id: $id, ')
          ..write('todoContent: $todoContent')
          ..write(')'))
        .toString();
  }

  @override
  int get hashCode => Object.hash(id, todoContent);
  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      (other is TaskViewData &&
          other.id == this.id &&
          other.todoContent == this.todoContent);
}
@simolus3
Copy link
Owner

Are the tables and the views in different drift files? If so, you need an import 'path/to/table.drift'; import statement at the top of the drift file importing the view.

@greenking19
Copy link
Author

Are the tables and the views in different drift files? If so, you need an import 'path/to/table.drift'; import statement at the top of the drift file importing the view.

The problem was resolved after importing the drift file.
I didn't read the document clearly. I found it on Supported statements

thank you ! 🥳

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

No branches or pull requests

2 participants