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

DatabaseView from Entities #785

Open
robotoss opened this issue Oct 25, 2023 · 0 comments
Open

DatabaseView from Entities #785

robotoss opened this issue Oct 25, 2023 · 0 comments

Comments

@robotoss
Copy link

robotoss commented Oct 25, 2023

Hello
I have 3 table
A:

const gameTableName = 'games';

@Entity(tableName: gameTableName)
class Game {
  const Game({
    required this.id,
    required this.alias,
    required this.name,
  });

  @primaryKey
  final String id;
  final String alias;
  final String name;
}

B:

const gamesCategoriesTableName = 'games_categories';

@Entity(tableName: gamesCategoriesTableName)
class GamesCategory {
  const GamesCategory({
    required this.id,
    required this.name,
    required this.alias,
  });

  @primaryKey
  final String id;
  final String name;
  final String alias;
}

C:

const gamesInCategoryTableName = 'game_in_categories';

@Entity(
  tableName: gamesInCategoryTableName,
  foreignKeys: [
    ForeignKey(
      childColumns: ['categoryId'],
      parentColumns: ['id'],
      entity: GamesCategory,
    ),
    ForeignKey(
      childColumns: ['gameId'],
      parentColumns: ['id'],
      entity: Game,
    ),
  ],
)
class GameInCategory {
  const GameInCategory({
    this.id,
    required this.order,
    required this.categoryId,
    required this.gameId,
  });

  @PrimaryKey(autoGenerate: true)
  final int? id;
  final int order;
  final String categoryId;
  final String gameId;
}

I write:

@DatabaseView(
  '''
    SELECT
        gic.order as order,
        gc.*,
        g.*
    FROM
        $gamesInCategoryTableName as gic
    INNER JOIN
        category as gc ON gic.categoryId = gc.id
    AS game INNER JOIN
        game as g ON gic.gameId = g.id
    ''',
  viewName: 'game_with_category',
)
class GameWithCategory {
  const GameWithCategory({
    required this.category,
    required this.order,
    required this.game,
  });

  final int order;
  final GamesCategory category;
  final Game game;
}
@Database(
  version: 1,
  views: [GameWithCategory],
  entities: [GamesCategory, Game, GameInCategory],
)
abstract class AppDatabase extends FloorDatabase {
  GamesCategoriesDao get gamesCategoriesDao;

  GamesDao get gamesDao;

  GameInCategoryDao get gameInCategoryDao;
}

But on generation I have errors:

Column type is not supported for GamesCategory.
Column type is not supported for Game.

How can I get class for use with DAO like this:

  @Query('SELECT * FROM game_with_category')
  Future<List<GameWithCategory>> getGamesWithCategory();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant