i have modles Staff.js
and RanchOwner.js
.
Staff has One to many relation with ranch_owners , i want to fetch all ranch owners related to staff
Staff.js
import { Model } from '@nozbe/watermelondb';
import { field, date, relation } from '@nozbe/watermelondb/decorators';
import moment from 'moment';
// import { children, readonly, lazy } from '@nozbe/watermelondb/decorators';
export default class Staff extends Model {
static table = 'staff';
@field('id') id;
@field('name') name;
@field('email') email;
@field('email_verified_at') emailVerifiedAt;
@field('password') password;
@field('fcm_token') fcmToken;
@field('api_token') apiToken;
@field('allowed_login') allowedLogin;
@field('status') status;
@field('admin') admin;
@field('owner_id') ownerId;
@field('role_id') role_id;
@field('subrole_id') subroleId;
@field('today_all_assigned_message_sent') todayAllAssignedMessageSent;
@field('tomorrow_all_assigned_message_sent') tomorrowAllAssignedMessageSent;
@field('today_all_gangs_assigned_message_sent') todayAllGangsAssignedMessageSent;
@field('tomorrow_all_gangs_assigned_message_sent') tomorrowAllGangsAssignedMessageSent;
@field('social_security_number') social_security_number;
@field('phone_number') phoneNumber;
@field('whatsapp') whatsapp;
@field('photo') photo;
@field('govt_id') govtId;
@field('tax_id') taxId;
@field('remember_token') rememberToken;
@field('is_temporary') isTemporary;
// @date('created_at') createdAt;
@date('updated_at') updatedAt;
@date('deleted_at') deletedAt;
@field('warehouse') warehouse;
@field('customer_id') customerId;
@field('is_driver') isDriver;
@field('online') online;
@field('location_access') locationAccess;
@field('qr_code') qrCode;
@field('color') color;
@field('belongs_to_staff') belongs_to_staff;
@date('created_at') _created_at;
get created_at() {
return moment(this._created_at).format('DD-MM-YYYY HH:mm:ss');
}
get role() {
let role = '';
switch (this.role_id) {
case 1:
role = 'agent';
break;
case 2:
role = 'ranch_owner';
break;
case 3:
role = 'worker';
break;
case 4:
role = 'right_hand_man';
break;
case 5:
role = 'independent_contractor';
break;
case 6:
role = 'packaging_company';
break;
case 7:
role = 'cutting_company';
break;
case 8:
role = 'supervisor';
break;
case 9:
role = 'gang_boss';
break;
case 10:
role = 'driver';
break;
case 11:
role = 'truck_assigner';
break;
case 12:
role = 'company_worker';
break;
default:
role = 'admin';
}
return role;
}
static associations = {
ranch_owners: { type: 'has_many', foreignKey: 'staff_id' }
};
@relation('ranch_owners', 'staff_id') ranch_owners;
// static associations = {
// ranch_owner: { type: 'belongs_to', key: 'staff_id' },
// }
// Relationships (if any)
// Example:
// @relation('another_table_name', 'foreign_key_column_name')
// anotherTable;
// Virtual fields (if any)
// Example:
}
RanchOwner.js
import { Model } from '@nozbe/watermelondb';
import { field, relation, writer } from '@nozbe/watermelondb/decorators';
// import moment from 'moment';
// import { children, readonly, lazy } from '@nozbe/watermelondb/decorators';
export default class RanchOwner extends Model {
static table = 'ranch_owners';
@field('id') id;
@field('tax_id') tax_id;
@field('compliance_opinion') compliance_opinion;
@field('address') address;
@field('latitude') latitude;
@field('longitude') longitude;
@field('city') city;
@field('hectares') hectares;
@field('ranch_name') ranch_name;
@field('person_id') person_id;
@field('govt_id') govt_id;
@field('general_manager') general_manager;
@field('staff_id') staffId;
@field('belongs_to_staff') belongs_to_staff;
static associations = {
staff: { type: 'belongs_to', key: 'staff_id' }
};
@relation('staff', 'staff_id') staff;
@writer async updateRelationWithStaff(staff_id) {
await this.batch(
this.prepareUpdate((ranchOwner) => {
ranchOwner.staffId = staff_id;
console.log(ranchOwner.staffId )
})
);
}
}
Query
const initialData = await database.get('staff').query(Q.take(20), Q.sortBy('created_at', Q.desc)).fetch();
But when i try to fetch relational data it gives object like this but having no error
I tried to change the name of relationship and try to make it one-to-one relation aswell. but in any case i can not see related may be something is wrong with naming convention