Nozbe / WatermelonDB

🍉 Reactive & asynchronous database for powerful React and React Native apps ⚡️
https://watermelondb.dev
MIT License
10.62k stars 600 forks source link

relations are not made on first sync #1613

Open bilalswiftsolutions opened 1 year ago

bilalswiftsolutions commented 1 year ago

I have two models one Staff.js other is RanchOwner.js

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('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;
  @date('created_at') _created_at;

  static associations = {
    ranch_owners: { type: 'belongs_to', foreignKey: 'staff_id' }
  };
  @relation('ranch_owners', 'staff_id') ranch_owner;

}

RanchOwner.js

import { Model } from '@nozbe/watermelondb';
import { field, relation } 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('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') staff_id;
  static associations = {
    staff: { type: 'belongs_to', foreignKey: 'staff_id' }
  };
  @relation('staff', 'staff_id') staff;
}

On the first sync both tables are having their on data but the relation is not made, do I need to do this manually?

Here is Schema

import { appSchema, tableSchema } from '@nozbe/watermelondb';

const schema = appSchema({
  version: 3,
  tables: [
    tableSchema({
      name: 'staff',
      columns: [
        { name: 'siloc_id', type: 'number',isOptional:false, },
        { name: 'name', type: 'string' },
        { name: 'email', type: 'string', isIndexed: true },
        { name: 'email_verified_at', type: 'number', isOptional: true },
        { name: 'password', type: 'string' },
        { name: 'fcm_token', type: 'string', isOptional: true },
        { name: 'api_token', type: 'string', isOptional: true, isIndexed: true },
        { name: 'allowed_login', type: 'boolean', isIndexed: true },
        { name: 'status', type: 'boolean', isIndexed: true },
        { name: 'admin', type: 'boolean', isIndexed: true },
        { name: 'owner_id', type: 'number', isOptional: true, isIndexed: true },
        { name: 'role_id', type: 'number', isIndexed: true },
        { name: 'subrole_id', type: 'number', isIndexed: true },
        { name: 'today_all_assigned_message_sent', type: 'boolean', isIndexed: true },
        { name: 'tomorrow_all_assigned_message_sent', type: 'boolean', isIndexed: true },
        { name: 'today_all_gangs_assigned_message_sent', type: 'boolean', isIndexed: true },
        { name: 'tomorrow_all_gangs_assigned_message_sent', type: 'boolean', isIndexed: true },
        { name: 'social_security_number', type: 'string', isOptional: true, isIndexed: true },
        { name: 'phone_number', type: 'string', isOptional: true, isIndexed: true },
        { name: 'whatsapp', type: 'number', isOptional: true },
        { name: 'photo', type: 'string', isOptional: true },
        { name: 'govt_id', type: 'string', isOptional: true, isIndexed: true },
        { name: 'tax_id', type: 'string', isOptional: true },
        { name: 'remember_token', type: 'string', isOptional: true },
        { name: 'is_temporary', type: 'boolean', isIndexed: true },
        { name: 'created_at', type: 'number' },
        { name: 'updated_at', type: 'number' },
        { name: 'deleted_at', type: 'number' },
        { name: 'warehouse', type: 'string', isOptional: true },
        { name: 'customer_id', type: 'number', isOptional: true },
        { name: 'is_driver', type: 'boolean', isIndexed: true },
        { name: 'online', type: 'boolean', isIndexed: true },
        { name: 'location_access', type: 'boolean', isIndexed: true },
        { name: 'qr_code', type: 'string', isOptional: true },
        { name: 'color', type: 'string', isOptional: true }
      ]
    }),
    tableSchema({
      name: 'ranch_owners',
      columns: [
        { name: 'siloc_id', type: 'number',isOptional:false, },
        { name: 'staff_id', type: 'string',isOptional:false },
        { name: 'tax_id', type: 'string', isOptional: true },
        { name: 'compliance_opinion', type: 'string', isOptional: true },
        { name: 'address', type: 'string', isOptional: true },
        { name: 'latitude', type: 'string', isOptional: true },
        { name: 'longitude', type: 'string', isOptional: true },
        { name: 'city', type: 'string', isOptional: true },
        { name: 'hectares', type: 'string', isOptional: true },
        { name: 'ranch_name', type: 'string', isOptional: true },
        { name: 'person_id', type: 'string', isOptional: true },
        { name: 'govt_id', type: 'string', isOptional: true },
        { name: 'general_manager', type: 'string', isOptional: true },
        { name: 'created_at', type: 'number', isOptional: false },
        { name: 'updated_at', type: 'number', isOptional: false },
      ],
    }),
  ]
});

export default schema;

If I have to do this manually please share some experience with me on how I can achieve it. I can use loop and update each records with sync but i am going to have thousands of records which can hand browser

bilalswiftsolutions commented 1 year ago

@KrisLau @radex

dskoneczny commented 1 year ago

Same issue on my end