diff --git a/Sources/MySQLStORM.swift b/Sources/MySQLStORM.swift index 4e68e65..129a538 100644 --- a/Sources/MySQLStORM.swift +++ b/Sources/MySQLStORM.swift @@ -265,42 +265,39 @@ open class MySQLStORM: StORM, StORMProtocol { var createStatement = str if str.characters.count == 0 { var opt = [String]() - var keyName = "" - for child in Mirror(reflecting: self).children { - guard let key = child.label else { - continue - } - var verbage = "" - if !key.hasPrefix("internal_") && !key.hasPrefix("_") { - verbage = "`\(key)` " - if child.value is Int && opt.count == 0 { - verbage += "int" - } else if child.value is Int { - verbage += "int" - } else if child.value is Bool { - verbage += "int" // MySQL has no bool type - } else if child.value is Double { - verbage += "float" - } else if child.value is UInt || child.value is UInt8 || child.value is UInt16 || child.value is UInt32 || child.value is UInt64 { - verbage += "blob" - } else { - verbage += "text" - } - if opt.count == 0 { - if child.value is Int { - verbage = "`\(key)` int NOT NULL AUTO_INCREMENT" - } else { - verbage = "`\(key)` varchar(255) NOT NULL" - } - keyName = key - } - opt.append(verbage) - } + let mirrorData = StORMMirrorData.mirror(mirror: Mirror(reflecting: self)) + var verbage = "" + for child in mirrorData.childs { + verbage = "`\(child.label)` " + if child.value is Int && opt.count == 0 { + verbage += "int" + } else if child.value is Int { + verbage += "int" + } else if child.value is Bool { + verbage += "int" // MySQL has no bool type + } else if child.value is Double { + verbage += "float" + } else if child.value is UInt || child.value is UInt8 || child.value is UInt16 || child.value is UInt32 || child.value is UInt64 { + verbage += "blob" + } else { + verbage += "text" + } + + opt.append(verbage) } - let keyComponent = ", PRIMARY KEY (`\(keyName)`)" - - createStatement = "CREATE TABLE IF NOT EXISTS \(table()) (\(opt.joined(separator: ", "))\(keyComponent));" - if StORMdebug { LogFile.info("createStatement: \(createStatement)", logFile: "./StORMlog.txt") } + if let primary = mirrorData.primary { + if primary.value is Int || primary.value is Int32 { + verbage = "`\(primary.label)` int NOT NULL AUTO_INCREMENT" + } else { + verbage = "`\(primary.label)` varchar(255) NOT NULL" + } + opt.insert(verbage, at: 0) + let keyComponent = ", PRIMARY KEY (`\(primary.label)`)" + createStatement = "CREATE TABLE IF NOT EXISTS \(table()) (\(opt.joined(separator: ", "))\(keyComponent));" + if StORMdebug { LogFile.info("createStatement: \(createStatement)", logFile: "./StORMlog.txt") } + } else { + LogFile.error("Con't find Primary Key") + } } do { try sql(createStatement, params: [])