struct TestUser: TableCodable {
var userId: String?
enum CodingKeys: String, CodingKey, CodingTableKey {
public typealias Root = TestUser
public static let objectRelationalMapping = TableBinding(CodingKeys.self) {
BindColumnConstraint(.userId, isPrimary: true)
BindIndex(userId, namedWith: "_uniqueIndex", isUnique: true)
}
case userId
}
}
func testCreateTable() {
let documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let dbPath = documentPath.appending("/test.db")
let database = WCDBSwift.Database(at: dbPath)
database.traceError { error in
if error.code == .NotADatabase, let path = error.infos[.path]?.stringValue {
try? FileManager.default.removeItem(at: URL(fileURLWithPath: path))
}
let ignoreError: [WCDBError.Level] = [.Ignore, .Debug]
if !ignoreError.contains(error.level) {
print("[WCDB](error)")
} else {
print("[WCDB](error)")
}
}
let queue = DispatchQueue(label: "test creat table", attributes: .concurrent)
for _ in 0 ... 1000 {
queue.async {
try? database.create(table: "user", of: TestUser.self)
}
}
}
报错信息:
[ERROR] Code:Error Message:table "user" has more than one primary key,Source:SQLite,Path:/var/mobile/Containers/Data/Application/B28C2919-E1A1-45DF-8386-10A1C0E02211/Documents/test.db,SQL:CREATE TABLE IF NOT EXISTS user(userId TEXT PRIMARY KEY PRIMARY KEY)
struct TestUser: TableCodable { var userId: String?
}
func testCreateTable() { let documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] let dbPath = documentPath.appending("/test.db") let database = WCDBSwift.Database(at: dbPath) database.traceError { error in if error.code == .NotADatabase, let path = error.infos[.path]?.stringValue { try? FileManager.default.removeItem(at: URL(fileURLWithPath: path)) } let ignoreError: [WCDBError.Level] = [.Ignore, .Debug] if !ignoreError.contains(error.level) { print("[WCDB](error)") } else { print("[WCDB](error)") } } let queue = DispatchQueue(label: "test creat table", attributes: .concurrent)
报错信息: [ERROR] Code:Error Message:table "user" has more than one primary key,Source:SQLite,Path:/var/mobile/Containers/Data/Application/B28C2919-E1A1-45DF-8386-10A1C0E02211/Documents/test.db,SQL:CREATE TABLE IF NOT EXISTS user(userId TEXT PRIMARY KEY PRIMARY KEY)