核心数据异常:向实体添加新属性后,SQLite“没有这样的列”错误

时间:2019-01-26 21:35:21

标签: core-data

在将名为“ localFilePath”的String属性添加到名为GenericAttachment的Core Data实体后,当我们尝试映射这些对象的Set时,我们开始遇到异常。

我们的几个实体与GenericAttachment的无序集合之间存在一对多的关系,称为“附件”。

这些实体之一称为InspectionMO。 GenericAttatchment与InspectionMO具有一对多的“检查”关系(不是“检查”,哎呀)。当我们获得InspectionMO的附件并尝试在其上进行映射时,会出现以下异常:

NSInternalInconsistencyException
I/O error for database at /Users/justingarcia/Library/Developer/CoreSimulator/Devices/D075B44C-4F54-4703-8817-3DC4A6E7314E/data/Containers/Data/Application/F425E5AC-C215-4BE8-93DB-A3E6C48C83C4/Library/Application Support/Procore/Procore.  SQLite error code:1, 'no such column: t1.Z_55ATTACHMENTS5'

这是异常堆栈跟踪:

#0  0x000000010c94f705 in objc_exception_throw ()
#1  0x000000010e928f00 in -[NSSQLiteConnection prepareSQLStatement:] ()
#2  0x000000010ea99305 in -[NSSQLiteConnection selectRowsWithStatement:cached:] ()
#3  0x000000010e941d0b in newFetchedRowsForFetchPlan_MT ()
#4  0x000000010eb6e574 in _newFetchedPKsForRelationshipFaultRequest ()
#5  0x000000010eb6efba in _executeNewValuesForRelationshipFaultRequest ()
#6  0x000000010ead19a2 in -[NSSQLRelationshipFaultRequestContext executeRequestCore:] ()
#7  0x000000010eb40a00 in -[NSSQLStoreRequestContext executeRequestUsingConnection:] ()
#8  0x000000010eb14e5b in __52-[NSSQLDefaultConnectionManager handleStoreRequest:]_block_invoke ()
#9  0x000000011b25f602 in _dispatch_client_callout ()
#10 0x000000011b26d653 in _dispatch_lane_barrier_sync_invoke_and_complete ()
#11 0x000000010eb14d40 in -[NSSQLDefaultConnectionManager handleStoreRequest:] ()
#12 0x000000010eb1cbb4 in -[NSSQLCoreDispatchManager routeStoreRequest:] ()
#13 0x000000010ea61515 in -[NSSQLCore dispatchRequest:withRetries:] ()
#14 0x000000010ea5cdaa in -[NSSQLCore _newValuesForRelationship:forObjectWithID:withContext:error:] ()
#15 0x000000010e97a8cb in -[NSSQLCore newValueForRelationship:forObjectWithID:withContext:error:] ()
#16 0x000000010ea449d9 in __110-[NSPersistentStoreCoordinator(_NSInternalMethods) newValueForRelationship:forObjectWithID:withContext:error:]_block_invoke ()
#17 0x000000010ea39437 in -[NSPersistentStoreCoordinator _routeLightweightBlock:toStore:] ()
#18 0x000000010e97a7f7 in -[NSPersistentStoreCoordinator(_NSInternalMethods) newValueForRelationship:forObjectWithID:withContext:error:] ()
#19 0x000000010ea0b89e in __107-[NSManagedObjectContext(_NestedContextSupport) newValueForRelationship:forObjectWithID:withContext:error:]_block_invoke ()
#20 0x000000010e972108 in internalBlockToNSManagedObjectContextPerform ()
#21 0x000000011b25f602 in _dispatch_client_callout ()
#22 0x000000011b26d653 in _dispatch_lane_barrier_sync_invoke_and_complete ()
#23 0x000000010e972074 in _perform ()
#24 0x000000010e972c37 in -[NSManagedObjectContext(_NestedContextSupport) newValueForRelationship:forObjectWithID:withContext:error:] ()
#25 0x000000010e97a421 in -[NSFaultHandler retainedFulfillAggregateFaultForObject:andRelationship:withContext:] ()
#26 0x000000010e9a621e in -[_NSFaultingMutableSet willReadWithContents:] ()
#27 0x000000010e94e6a6 in -[_NSFaultingMutableSet count] ()
#28 0x000000011a9e9bdd in protocol witness for Collection.count.getter in conformance Set<A> ()
#29 0x000000011a8726d1 in Collection.map<A>(_:) ()
#30 0x00000001162ad07e in ObjectImportHelper.copyToMany<A>(_:to:cache:deleteMissing:onCopy:) at /Users/justingarcia/2/iOS/Procore/Core/Utils/ObjectImportHelper+Relationships.swift:163
…

以下是一些核心数据记录:

CoreData: sql: SELECT 0, t0.Z_PK FROM Z_55INSPECTION t1 JOIN ZGENERICATTACHMENT t0 ON t0.Z_PK = t1.Z_55ATTACHMENTS5 WHERE t1.Z_70INSPECTION = ? 
2019-01-25 13:50:17.727987-0800 Procore[34877:10124838] [logging] no such column: t1.Z_55ATTACHMENTS5
CoreData: annotation: Disconnecting from sqlite database due to an error.
2019-01-25 13:50:17.748021-0800 Procore[34877:10124838] [error] error: (1) I/O error for database at /var/mobile/Containers/Data/Application/FFEB392B-302B-479E-98E8-008333FC2714/Library/Application Support/Procore/Procore.  SQLite error code:1, 'no such column: t1.Z_55ATTACHMENTS5'
CoreData: error: (1) I/O error for database at /var/mobile/Containers/Data/Application/FFEB392B-302B-479E-98E8-008333FC2714/Library/Application Support/Procore/Procore.  SQLite error code:1, 'no such column: t1.Z_55ATTACHMENTS5'
CoreData: annotation: total fetch execution time: 0.0207s for 0 rows.
2019-01-25 13:50:17.748727-0800 Procore[34877:10124838] [error] error: exception during newFetchedPKsForSourceID: I/O error for database at /var/mobile/Containers/Data/Application/FFEB392B-302B-479E-98E8-008333FC2714/Library/Application Support/Procore/Procore.  SQLite error code:1, 'no such column: t1.Z_55ATTACHMENTS5' with userInfo of {
    NSFilePath = "/var/mobile/Containers/Data/Application/FFEB392B-302B-479E-98E8-008333FC2714/Library/Application Support/Procore/Procore";
    NSSQLiteErrorDomain = 1;
}

我们正在使用NSPersistentStoreDescription的shouldMigrateStoreAutomatically和ShouldInferMappingModelAutomatically,并且在日志中没有看到任何迁移错误(我们正在使用-com.apple.CoreData.MigrationDebug 1)。

我们打开了SQLite数据库文件,发现Z_55INSPECTION表在迁移到此新版本之前和之后都有名为Z_55ATTACHMENTS6(而不是Z_55ATTACHMENTS5)的列,在此我们将localFilePath属性添加到GenericAttachment。

那么核心数据为什么要寻找名为Z_55ATTACHMENTS5而不是Z_55ATTACHMENTS6的列?

我们在谷歌搜索之后发现了这两篇苹果邮件列表文章:a questionthis follow-up,我认为这是说当多个实体具有多对多关系时,列名末尾的数字就会出现与另一个实体同名。在我们的案例中,我们有几个多对多关系,称为GenericAttachment的附件。但是我们没有添加名为附件的新关系,只是向GenericAttachment添加了一个属性。也许如果我们将所有这些关系重命名为唯一的关​​系,问题将会消失吗?

1 个答案:

答案 0 :(得分:0)

它选择该列,因为查询指向Z_55INSPECTION.Z_55ATTACHMENTS5

SELECT 0, t0.Z_PK FROM Z_55INSPECTION t1

JOIN ZGENERICATTACHMENT t0 ON t0.Z_PK = t1.Z_55ATTACHMENTS5

WHERE t1.Z_70INSPECTION = ?

最简单的方法可能是将该列从Z_55ATTACHMENTS6重命名为Z_55ATTACHMENTS5

...保留数据库的备份,以确保没有其他引用。