Skip to content

Commit

Permalink
Merge pull request #185 from vapor/primary-key-bug
Browse files Browse the repository at this point in the history
Primary key bug
  • Loading branch information
tanner0101 committed Feb 16, 2017
2 parents 14e9855 + 143ca06 commit c7ef296
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Sources/Fluent/Entity/Entity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ extension Entity {

/// See Entity.idType
public static var idType: IdentifierType {
return database?.driver.idType ?? .uuid
return database?.driver.idType ?? .int
}

/// See Entity.idKey
Expand Down
10 changes: 7 additions & 3 deletions Sources/Fluent/SQL/GeneralSQLSerializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ open class GeneralSQLSerializer: SQLSerializer {
var clause: [String] = []

clause += sql(column.name)
clause += sql(column.type)
clause += sql(column.type, primaryKey: column.primaryKey)

if !column.optional {
clause += "NOT NULL"
Expand Down Expand Up @@ -402,7 +402,7 @@ open class GeneralSQLSerializer: SQLSerializer {
}


open func sql(_ type: Schema.Field.DataType) -> String {
open func sql(_ type: Schema.Field.DataType, primaryKey: Bool) -> String {
switch type {
case .id(let type):
let typeString: String
Expand All @@ -414,7 +414,11 @@ open class GeneralSQLSerializer: SQLSerializer {
case .custom(let dataType):
typeString = dataType
}
return typeString + " PRIMARY KEY"
if primaryKey {
return typeString + " PRIMARY KEY"
} else {
return typeString
}
case .int:
return "INTEGER"
case .string(_):
Expand Down
3 changes: 2 additions & 1 deletion Sources/Fluent/Schema/Schema+Creator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ extension Schema {
public func id<E: Entity>(for entityType: E.Type) {
fields += Field(
name: E.idKey,
type: .id(type: E.idType)
type: .id(type: E.idType),
primaryKey: true
)
}

Expand Down
21 changes: 13 additions & 8 deletions Sources/Fluent/Schema/Schema+Field.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ extension Schema {
/// Various types of fields
/// that can be used in a Schema.
public struct Field {
public var name: String
public var type: DataType
public var optional: Bool
public var unique: Bool
public var `default`: Node?
public let name: String
public let type: DataType
public let optional: Bool
public let unique: Bool
public let `default`: Node?
public let primaryKey: Bool

public enum DataType {
case id(type: IdentifierType)
Expand All @@ -23,21 +24,24 @@ extension Schema {
type: DataType,
optional: Bool = false,
unique: Bool = false,
default: Node? = nil
default: Node? = nil,
primaryKey: Bool = false
) {
self.name = name
self.type = type
self.optional = optional
self.unique = unique
self.default = `default`
self.primaryKey = primaryKey
}

public init(
name: String,
type: DataType,
optional: Bool = false,
unique: Bool = false,
default: NodeRepresentable? = nil
default: NodeRepresentable? = nil,
primaryKey: Bool = false
) {
let node: Node?

Expand All @@ -52,7 +56,8 @@ extension Schema {
type: type,
optional: optional,
unique: unique,
default: node
default: node,
primaryKey: primaryKey
)
}
}
Expand Down

0 comments on commit c7ef296

Please sign in to comment.