title: How to create foreign key in sqlite.swift using Data Model
body: <p>I am using sqlite.swift for database creation. I have created a class using with the help of this link <a href="http://masteringswift.blogspot.in/2015/09/create-data-access-layer-with.html" rel="nofollow">http://masteringswift.blogspot.in/2015/09/create-data-access-layer-with.html</a>. I want to create a foreign key on teamID(column) in player table reference as team table teamID(column).</p>
  
  <p>Here is the helper class for team and player table:</p>
  
  <pre><code>class TeamDataHelper: DataHelperProtocol {
  static let TABLE_NAME = "Teams"
  
  static let table = Table(TABLE_NAME)
  static let teamId = Expression&lt;Int64&gt;("teamid")
  static let city = Expression&lt;String&gt;("city")
  static let nickName = Expression&lt;String&gt;("nickname")
  static let abbreviation = Expression&lt;String&gt;("abbreviation")
  
  
  typealias T = Team
  
  static func createTable() throws {
      guard let DB = SQLiteDataStore.sharedInstance.BBDB else {
          throw DataAccessError.Datastore_Connection_Error
      }
      do {
          let _ = try DB.run( table.create(ifNotExists: true) {t in
              t.column(teamId, primaryKey: true)
              t.column(city)
              t.column(nickName)
              t.column(abbreviation)
              })
  
      } catch _ {
          // Error throw if table already exists
      }
  
  }
  
  static func insert(item: T) throws -&gt; Int64 {
      guard let DB = SQLiteDataStore.sharedInstance.BBDB else {
          throw DataAccessError.Datastore_Connection_Error
      }
      if (item.city != nil &amp;&amp; item.nickName != nil &amp;&amp; item.abbreviation != nil) {
          let insert = table.insert(city &lt;- item.city!, nickName &lt;- item.nickName!, abbreviation &lt;- item.abbreviation!)
          do {
              let rowId = try DB.run(insert)
              guard rowId &gt; 0 else {
                  throw DataAccessError.Insert_Error
              }
              return rowId
          } catch _ {
              throw DataAccessError.Insert_Error
          }
      }
      throw DataAccessError.Nil_In_Data
  
  }
  
  static func delete (item: T) throws -&gt; Void {
      guard let DB = SQLiteDataStore.sharedInstance.BBDB else {
          throw DataAccessError.Datastore_Connection_Error
      }
      if let id = item.teamId {
          let query = table.filter(teamId == id)
          do {
              let tmp = try DB.run(query.delete())
              guard tmp == 1 else {
                  throw DataAccessError.Delete_Error
              }
          } catch _ {
              throw DataAccessError.Delete_Error
          }
      }
  }
  
  static func find(id: Int64) throws -&gt; T? {
      guard let DB = SQLiteDataStore.sharedInstance.BBDB else {
          throw DataAccessError.Datastore_Connection_Error
      }
      let query = table.filter(teamId == id)
      let items = try DB.prepare(query)
      for item in  items {
          return Team(teamId: item[teamId] , city: item[city], nickName: item[nickName], abbreviation: item[abbreviation])
      }
  
      return nil
  
  }
  
  static func findAll() throws -&gt; [T]? {
      guard let DB = SQLiteDataStore.sharedInstance.BBDB else {
          throw DataAccessError.Datastore_Connection_Error
      }
      var retArray = [T]()
      let items = try DB.prepare(table)
      for item in items {
          retArray.append(Team(teamId: item[teamId], city: item[city], nickName: item[nickName], abbreviation: item[abbreviation]))
      }
  
      return retArray
  
  }
  }
  
  
  
  class PlayerDataHelper: DataHelperProtocol {
  static let TABLE_NAME = "Players"
  
  static let playerId = Expression&lt;Int64&gt;("playerid")
  static let firstName = Expression&lt;String&gt;("firstName")
  static let lastName = Expression&lt;String&gt;("lastName")
  static let number = Expression&lt;Int&gt;("number")
  static let teamId = Expression&lt;Int64&gt;("teamid")
  static let position = Expression&lt;String&gt;("position")
  
  
  static let table = Table(TABLE_NAME)
  
  typealias T = Player
  
  static func createTable() throws {
      guard let DB = SQLiteDataStore.sharedInstance.BBDB else {
          throw DataAccessError.Datastore_Connection_Error
      }
      do {
          _ = try DB.run( table.create(ifNotExists: true) {t in
  
              t.column(playerId, primaryKey: true)
              t.column(firstName)
              t.column(lastName)
              t.column(number)
              t.column(teamId)
              t.column(position)
  
  
              })
      } catch _ {
          // Error thrown when table exists
      }
  }
  
  static func insert(item: T) throws -&gt; Int64 {
      guard let DB = SQLiteDataStore.sharedInstance.BBDB else {
          throw DataAccessError.Datastore_Connection_Error
      }
      if (item.firstName != nil &amp;&amp; item.lastName != nil &amp;&amp; item.teamId != nil &amp;&amp; item.position != nil) {
          let insert = table.insert(firstName &lt;- item.firstName!, number &lt;- item.number!, lastName &lt;- item.lastName!, teamId &lt;- item.teamId!, position &lt;- item.position!.rawValue)
          do {
              let rowId = try DB.run(insert)
              guard rowId &gt;= 0 else {
                  throw DataAccessError.Insert_Error
              }
              return rowId
          } catch _ {
              throw DataAccessError.Insert_Error
          }
      }
      throw DataAccessError.Nil_In_Data
  }
  
  static func delete (item: T) throws -&gt; Void {
      guard let DB = SQLiteDataStore.sharedInstance.BBDB else {
          throw DataAccessError.Datastore_Connection_Error
      }
      if let id = item.playerId {
          let query = table.filter(playerId == id)
          do {
              let tmp = try DB.run(query.delete())
              guard tmp == 1 else {
                  throw DataAccessError.Delete_Error
              }
          } catch _ {
              throw DataAccessError.Delete_Error
          }
      }
  
  }
  
  static func find(id: Int64) throws -&gt; T? {
      guard let DB = SQLiteDataStore.sharedInstance.BBDB else {
          throw DataAccessError.Datastore_Connection_Error
      }
      let query = table.filter(playerId == id)
      let items = try DB.prepare(query)
      for item in  items {
          return Player(playerId: item[playerId], firstName: item[firstName], lastName: item[lastName], number: item[number], teamId: item[teamId], position: Positions(rawValue: item[position]))
      }
  
      return nil
  
  }
  
  static func findAll() throws -&gt; [T]? {
      guard let DB = SQLiteDataStore.sharedInstance.BBDB else {
          throw DataAccessError.Datastore_Connection_Error
      }
      var retArray = [T]()
      let items = try DB.prepare(table)
      for item in items {
          retArray.append(Player(playerId: item[playerId], firstName: item[firstName], lastName: item[lastName], number: item[number], teamId: item[teamId], position: Positions(rawValue: item[position])))
      }
  
      return retArray
  }
  }
  </code></pre>
  
  <p>In above player table I want to create foreign key for teamID as reference team table teamID. If I am creating foreign key like"t.foreignKey(teamID, on: TeamDataHelper.teamID)" it is giving error. </p>
  
  <p>can anyone suggest me any help. </p>