WatermelonDB icon indicating copy to clipboard operation
WatermelonDB copied to clipboard

Fetching along with linked records

Open ragsav opened this issue 3 years ago • 1 comments

Hello Team, I am new to watermelon DB and was stuck on one use case. What if I have a two records

Transaction: id, account_id, amount, time Account: id, title, description

I need to fetch all the transactions along with the accounts which are linked to them, not just the relation. How can it be achieved. Your suggestion would be helpful for me Thank you

// code for transaction model static associations = { accounts: {type: 'belongs_to', key: 'id'}, transaction_categories: {type: 'belongs_to', key: 'id'}, };

@text('title') title; @text('description') description; @text('transaction_type') transactionType; @text('transaction_category_id') transactionCategoryID; @text('account_id') accountID; @date('input_time') inputTime; @field('amount') amount;

@readonly @date('created_at') createdAt; @readonly @date('updated_at') updatedAt;

@relation('accounts', 'account_id') account; @relation('transaction_categories', 'transaction_category_id') transactionCategory;

// code for accounts model

static associations = { transactions: {type: 'has_many', foreignKey: 'account_id'}, };

@text('title') title; @text('description') description; @text('currency') currency;

@readonly @date('created_at') createdAt; @readonly @date('updated_at') updatedAt;

@children('transactions') transactions;`

ragsav avatar Aug 21 '22 17:08 ragsav

@radex can you please suggest a way to achieve this?

ragsav avatar Aug 21 '22 19:08 ragsav