Tencent / wcdb

WCDB is a cross-platform database framework developed by WeChat.
Other
10.78k stars 1.41k forks source link

请问为什么多线程触发创建同一张表,创建表的sql会拼接错误 #1116

Closed 374714754 closed 3 months ago

374714754 commented 3 months ago

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)

374714754 commented 3 months ago

版本号: WCDB (2.1.2)

Qiuwen-chen commented 3 months ago

It is caused by sqlite.