简体   繁体   English

rxdb:文档迁移失败最终文档与最终架构不匹配

[英]rxdb: migration of document failed final document does not match final schema

I just changed my rxdb schema version from 0 to 1 in order to add a deletedAt attribute.我只是将我的 rxdb 架构版本从 0 更改为 1,以便添加一个deletedAt属性。 I added a migration strategy for going from ver 0 to 1.我添加了从 ver 0 到 1 的迁移策略。

Now I'm getting this error: "migration of document failed final document does not match final schema".现在我收到此错误: “文档迁移失败的最终文档与最终架构不匹配”。 The final doc is in screenshot below:最终文档如下图所示:

来自使用 rxdb 的反应应用程序的控制台错误

I thought maybe I had to add the _rev field;我想也许我必须添加_rev字段; adding _rev to schema didn't remove the error, so I've taken it back out._rev添加到架构并没有消除错误,所以我已经把它拿回来了。 Ditto with a deleted field (complains I can't add it as a top-level attribute).同样带有deleted字段(抱怨我无法将其添加为顶级属性)。 So I am at a loss as to why else the final object differs from the expected schema?所以我不知道为什么最终的对象与预期的模式不同?

type info输入信息

export type TodoType = {
  id: string
  text: string
  isCompleted: boolean
  createdAt: string
  updatedAt: string
  deletedAt: string
}
//...
export const todoSchema: RxJsonSchema<TodoType> = {
  title: 'todo schema',
  description: 'todo schema',
  version: 1, // just changed this from 0 to 1
  type: 'object',
  properties: {
    id: {
      type: 'string',
      primary: true
    },
    text: {
      type: 'string'
    },
    isCompleted: {
      type: 'boolean'
    },
    createdAt: {
      type: 'string',
      format: 'date-time',
      // index: true,   
    },
    updatedAt: {
      type: 'string',
      format: 'date-time'
    },
    deletedAt: {
      type: 'string',
      format: 'date-time'
    },
  },
  required: ['id', 'text', 'isCompleted', 'createdAt', 'updatedAt', 'deletedAt'],
  indexes: ['createdAt']
}

migrator code迁移代码

  await myDatabase.collection({
    name: 'todos',
    schema: todoSchema,
    methods: todoMethods,
    statics: todoCollectionMethods,
    migrationStrategies: {
      // 1 means, this transforms data from version 0 to version 1
      1: function(oldDoc: TodoDocument) {
        oldDoc.updatedAt = oldDoc.updatedAt === '' ? oldDoc.createdAt : oldDoc.updatedAt
        oldDoc.deletedAt = oldDoc.deleted ? oldDoc.updatedAt : ''
        return oldDoc;
      }
    }
  })

The problem was that deletedAt , which I had just added, was defined like this in the json schema:问题是我刚刚添加的deletedAt在 json 模式中是这样定义的:

    deletedAt: {
      type: 'string',
      format: 'date-time'
    },

... and I was defaulting the value to '' (empty string) when I was creating version 0 objects. ...当我创建版本 0 对象时,我将值默认为'' (空字符串)。 And an empty string is invalid for date-time format in json schema.并且空字符串对于 json 模式中的日期时间格式无效。 So when the objects were being migrated to version 1, the transform ended up with an object that had deletedAt , which doesn't pass validation.因此,当对象迁移到版本 1 时,转换最终得到一个已deletedAt的对象,该对象未通过验证。

Solution:解决方案:

Not sure why some kind of validation didn't kick in while saving the version 0 objects, only when it came time to migrate them.不知道为什么在保存版本 0 对象时没有启动某种验证,只有在迁移它们时才启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM