[英]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:
最终文档如下图所示:
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?所以我不知道为什么最终的对象与预期的模式不同?
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']
}
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
的对象,该对象未通过验证。
updatedAt
from the required: [...]
array.required: [...]
数组中删除updatedAt
来更新 rxdb 集合的json 模式。deletedAt
is an optional attribute or one that defaults to null.deletedAt
是一个可选属性或默认为 null 的属性。 This is okay because null is valid date-time format string in json schema . 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.