Skip to content

dyamic tables in query #2783

Closed Answered by simolus3
BananaMasterz asked this question in Q&A
Dec 12, 2023 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

Sure, drift exposes the schema at runtime and this can be used to craft dynamic queries.

final table = switch (tableNum) {
  1 => table1,
  2 => table2,
  3 => table3,
  _ => throw 'todo',
};

final mainTableId = table.columnsByName['main_table_id'] as GeneratedColumn<int>;
final commonCol = table.columnsByName['common_col'] as GeneratedColumn<String>;

select(mainTable, distinct: true).join([
      innerJoin(table , mainTableId.equalsExp(mainTable.id)),
])..where(commonCol.equals('query');

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@BananaMasterz
Comment options

Answer selected by BananaMasterz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants