UNIQUE 约束失败:ZTEMPORADA.Z_PK
Posted
技术标签:
【中文标题】UNIQUE 约束失败:ZTEMPORADA.Z_PK【英文标题】:UNIQUE constraint failed: ZTEMPORADA.Z_PK 【发布时间】:2015-08-23 17:51:42 【问题描述】:我正在做 swift 和 CoreData 的工作,但是在与“Temporada”执行“时间”关系时遇到问题,其中一个团队可以有几个赛季,一个赛季就是一个团队。
上课时间
import Foundation
import CoreData
@objc(Time)
class Time: NSManagedObject
@NSManaged var nome: String
@NSManaged var temporada: NSSet
override init(entity: NSEntityDescription, insertIntoManagedObjectContext context: NSManagedObjectContext?)
let entity:NSEntityDescription = CoreDataManager.getEntity("Time")
super.init(entity: entity, insertIntoManagedObjectContext: nil)
class func entityDescription() -> (NSEntityDescription)
let entity:NSEntityDescription = CoreDataManager.getEntity("Time")
return entity
func salvar()
let context:NSManagedObjectContext = CoreDataManager.getContext()
var error:NSError?
if (!self.inserted)
context.insertObject(self)
context.save(&error)
if (error != nil)
NSLog(error!.description)
类 Temporada
import Foundation
import CoreData
@objc(Temporada)
class Temporada: NSManagedObject
@NSManaged var ano: NSNumber
@NSManaged var numeroJogos: NSNumber
//relacionamentos
@NSManaged var time: Time
@NSManaged var jogador_temporada: NSSet
override init(entity: NSEntityDescription, insertIntoManagedObjectContext context: NSManagedObjectContext?)
let entity:NSEntityDescription = CoreDataManager.getEntity("Temporada")
super.init(entity: entity, insertIntoManagedObjectContext: nil)
class func entityDescription() -> (NSEntityDescription)
let entity:NSEntityDescription = CoreDataManager.getEntity("Temporada")
return entity
func salvar()
let context:NSManagedObjectContext = CoreDataManager.getContext()
var error:NSError?
if (!self.inserted)
context.insertObject(self)
context.save(&error)
if (error != nil)
NSLog(error!.description)
核心数据管理器
import Foundation
import CoreData
import UIKit
class CoreDataManager
class func getEntity(entidade: String) -> NSEntityDescription
let delegate = (UIApplication.sharedApplication()).delegate as! AppDelegate
let context:NSManagedObjectContext? = delegate.managedObjectContext
let description: NSEntityDescription = NSEntityDescription.entityForName(entidade, inManagedObjectContext: context!)!
return description
class func getContext() -> NSManagedObjectContext
let delegate = (UIApplication.sharedApplication()).delegate as! AppDelegate
return delegate.managedObjectContext!
class func getAllManagedObjectsFromEntity(entity: NSEntityDescription) -> (sucesso: Bool, objects: NSArray)
let delegate = (UIApplication.sharedApplication()).delegate as! AppDelegate
let context:NSManagedObjectContext? = delegate.managedObjectContext
let request:NSFetchRequest = NSFetchRequest()
request.entity = entity
var error:NSError?
var objects:NSArray? = context?.executeFetchRequest(request, error: &error)
if(error == nil)
return(true, objects!)
else
NSLog(error!.description)
return(false, objects!)
在我的 ViewController 中,我有两种方法,一种用于保存“时间”,另一种用于建立这种关系...
@IBAction func salvar(sender: AnyObject)
var time: Time = Time(entity: Time.entityDescription(), insertIntoManagedObjectContext: nil)
time.nome = "Cruzeiro Esporte Clube"
time.salvar()
NSLog("Time Salvo")
@IBAction func salvarTemporada(sender: AnyObject)
var time: Time?
var temporada: Temporada = Temporada(entity: Temporada.entityDescription(), insertIntoManagedObjectContext: nil)
temporada.ano = NSNumber(integer: 2017)
temporada.numeroJogos = NSNumber(integer: 10)
temporada.salvar()
var retorno = CoreDataManager.getAllManagedObjectsFromEntity(Time.entityDescription())
if (retorno.sucesso)
time = retorno.objects.objectAtIndex(0) as? Time
NSLog("recuperado = \(time?.nome)")
temporada.time = time!
temporada.salvar()
但我收到以下错误
2015-08-23 14:36:46.718 Kickoff_CoreData[1230:37874] CoreData: error: (1555) UNIQUE constraint failed: ZTEMPORADA.Z_PK
2015-08-23 14:36:46.719 Kickoff_CoreData[1230:37874] Core Data: error: -executeRequest: encountered exception = error during SQL execution : UNIQUE constraint failed: ZTEMPORADA.Z_PK with userInfo =
NSFilePath = "/Users/jonathanribeiro/Library/Developer/CoreSimulator/Devices/79DFD839-4790-40C4-8E92-59EB9ADED5CD/data/Containers/Data/Application/1CEB63B6-3AA8-414D-810F-46EEDC910270/Documents/Kickoff_CoreData.sqlite";
NSSQLiteErrorDomain = 1555;
谁能帮帮我?
【问题讨论】:
我认为错误是由于在代码中使用“Cruzeiro”引起的。如果您使用“Vasco”,您会更快乐。 【参考方案1】:我最近遇到了类似的问题。我发现我在NSManagedObjectContext
中插入了NSManagedObject
两次,然后尝试保存它。我看到您在同一方法中调用了两次temporada.salvar
,并且由于保存实际上是异步的,因此可能会造成麻烦。希望对您有所帮助。
【讨论】:
以上是关于UNIQUE 约束失败:ZTEMPORADA.Z_PK的主要内容,如果未能解决你的问题,请参考以下文章
模型 unique_together 约束 + 无 = 失败?