【问题标题】:Not create multiple table in Realm.不在 Realm 中创建多个表。
【发布时间】:2016-10-16 10:21:00
【问题描述】:

我正在使用 React native 创建 Realm 数据库表。我创建表的功能是,

const Realm = require('realm');

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

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

  return realm;
};

我调用了这个方法,

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",
     } );

我在defualt.realm 文件中只有MstUnitType 表,而我在3 个创建表方法上运行时未创建其他2 个表。

【问题讨论】:

    标签: react-native realm realm-mobile-platform


    【解决方案1】:

    是的,我找到了上述解决方案。按照这种方式,我们可以一次创建多个表,

    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]});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-25
      • 2016-08-17
      • 1970-01-01
      • 2021-04-24
      相关资源
      最近更新 更多