Search code examples
react-nativerealmrealm-mobile-platform

Not create multiple table in Realm.


I'm creating tables of Realm database using React native. My function of creating table is,

const Realm = require('realm');

exports.createTables = function(tableName, pkey, structure) {

  let realm = new Realm({
     schema: [{name: tableName, primaryKey: pkey, properties: structure}]
   });

  return realm;
};

and i calling this method,

import realmDatabase from './RealmDatabase';

   realmDatabase.createTables("MstUnitType", "UnitTypeId", {
       "UnitTypeName"           : "string",
       "UnitTypeId"             : "string",
     } );

     realmDatabase.createTables("MstTime", "TimeId", {
       "TimeId"           : "string",
       "From"             : "string",
       "To"               : "string",
     } );

    realmDatabase.createTables("MstQuestions", "QuestionId", {
       "QuestionId"           : "string",
       "Question"             : "string",
     } );

I got only MstUnitType table in defualt.realm file other 2 table not created while i run above 3 create table methods one by one.


Solution

  • Yes i found solution of above. Following way we can create multiple tables at a time,

    var Realm = require('realm');
    
    const CarSchema = {
      name: 'Car',
      properties: {
        make:  'string',
        model: 'string',
        miles: {type: 'int', default: 0},
      }
    };
    const PersonSchema = {
      name: 'Person',
      properties: {
        name:     'string',
        birthday: 'date',
        cars:     {type: 'list', objectType: 'Car'},
        picture:  {type: 'data', optional: true}, // optional property
      }
    };
    
    // Initialize a Realm with Car and Person models
    let realm = new Realm({schema: [CarSchema, PersonSchema]});