SQLite.swift(Swift 2.0)联接不显示所有字段

时间:2015-09-14 07:39:59

标签: join swift2 sqlite.swift

在将应用程序迁移到swift2时,我遇到了与SQLite.swift创建连接的问题。

在这种情况下,1.2中完美的功能在2.0中不起作用。可能是一个错误,或者只是更新语法的问题,但无法弄明白。

我正在执行几个表的连接:

    // Prep tables
    let equipment = Table("Equipment")
    let equ_id = Expression<Int>("Equipment_ID")
    let equ_type = Expression<Int>("EquipmentType_ID")
    let equ_owner = Expression<Int>("Equipment_Owner_ID")
    let equ_manufacturer = Expression<String>("Manufacturer")
    let equ_model = Expression<String>("Model")
    let equ_serial = Expression<String>("Serial")
    let equ_notes = Expression<String>("Notes")
    let equ_facid = Expression<Int>("Facility_ID")
    let equ_depid = Expression<Int>("Department_ID")
    let equ_cctid = Expression<Int>("CostCenter_ID")
    let equ_location = Expression<String>("Location_Details")
    let equ_status = Expression<Int>("Status")
    let equ_esttime = Expression<Int>("Planned_Maint_Est_Time")

    let facilities = Table("Facilities")
    let fac_id = Expression<Int>("Facility_ID")
    let fac_name = Expression<String>("Facility_Name")

    let equipment_type = Table("EquipmentTypes")
    let eqt_id = Expression<Int>("EquipmentType_ID")
    let eqt_type = Expression<String>("Equipment_Type")

    let preUsers = Table("PreUsers")
    let Worker_ID = Expression<Int>("Worker_ID")
    let First_Name = Expression<String>("First_Name")
    let Last_Name = Expression<String>("Last_Name")
    let Login = Expression<String>("Login")
    let Client_ID = Expression<Int>("Client_ID")
    let Company_ID = Expression<Int>("Company_ID")
    let Date_LastLogin = Expression<NSDate>("Date_LastLogin")

    // Load Initial Equipment Table

    let ext_equipment = equipment.join(equipment_type, on: equipment_type[eqt_id] == equipment[equ_type])
        .join(facilities, on: facilities[fac_id] == equipment[equ_facid])
        .join(preUsers, on: preUsers[Worker_ID] == equipment[equ_owner])
        .order(equ_model)
        .filter(equ_status >= 0)

之前工作正常(必须更改为新的表结构),但现在要么我做错了,要么加入找不到我的字段:

    for item in db.prepare(ext_equipment)
    {

        //print("ID: \(item[equ_id]), Make: \(item[equ_model]), Type: \(item[equipment_type[eqt_type]]), Fac: \(item[facilities[fac_name]])")
        //print("Owner: \(item[preUsers[First_Name]]) \(item[preUsers[Last_Name]])")

        var iID: Int!
        iID = item[equ_id]

        print(iID)

        var eqTemp = Equipment()
        eqTemp.eq_ID = iID
        eqTemp.eq_Manufacturer = item[equ_manufacturer]
        eqTemp.eq_Model = item[equ_model]
        eqTemp.eq_Serial = item[equ_serial]
        eqTemp.eq_FacilityID = item[facilities[fac_id]]
        eqTemp.eq_Facility = item[facilities[fac_name]]
        eqTemp.eq_TypeID = item[equipment_type[eqt_id]]
        eqTemp.eq_Type = item[equipment_type[eqt_type]]
        eqTemp.eq_OwnerID = item[preUsers[Worker_ID]]
        eqTemp.eq_Owner = item[preUsers[First_Name]] + " " + item[preUsers[Last_Name]]
        eqTemp.eq_Location = item[equ_location]
        eqTemp.eq_Status = item[equ_status]
        eqTemp.eq_Notes = item[equ_notes]

        allEquip.append(eqTemp)

    }

我还尝试将语法修改为:

        eqTemp.eq_FacilityID = item.get(facilities[fac_id])
        eqTemp.eq_Facility = item.get(facilities[fac_name])

但仍然没有运气。

引发致命错误:

fatal error: no such column '"Facilities"."Facility_ID"' in columns:

[
"Expression<()>(template: \"\\\"EquipmentTypes\\\"\", bindings: []).\"Client_ID\"", 
"Expression<()>(template: \"\\\"EquipmentTypes\\\"\", bindings: []).\"EquipmentType_ID\"", 
"Expression<()>(template: \"\\\"EquipmentTypes\\\"\", bindings: []).\"Equipment_Type\"", 
"Expression<()>(template: \"\\\"Equipment\\\"\", bindings: []).\"Client_ID\"", 
"Expression<()>(template: \"\\\"Equipment\\\"\", bindings: []).\"CostCenter_ID\"", 
"Expression<()>(template: \"\\\"Equipment\\\"\", bindings: []).\"Date_Created\"", 
"Expression<()>(template: \"\\\"Equipment\\\"\", bindings: []).\"Date_Decomisioned\"", 
"Expression<()>(template: \"\\\"Equipment\\\"\", bindings: []).\"Date_LastMaintenance\"", 
"Expression<()>(template: \"\\\"Equipment\\\"\", bindings: []).\"Date_NextMaintenance\"", 
"Expression<()>(template: \"\\\"Equipment\\\"\", bindings: []).\"Date_of_First_Use\"", 
"Expression<()>(template: \"\\\"Equipment\\\"\", bindings: []).\"Date_of_Purchase\"", 
"Expression<()>(template: \"\\\"Equipment\\\"\", bindings: []).\"Default_Priority\"", 
"Expression<()>(template: \"\\\"Equipment\\\"\", bindings: []).\"Department_ID\"", 
"Expression<()>(template: \"\\\"Equipment\\\"\", bindings: []).\"EquipmentType_ID\"", 
"Expression<()>(template: \"\\\"Equipment\\\"\", bindings: []).\"Equipment_ID\"", 
"Expression<()>(template: \"\\\"Equipment\\\"\", bindings: []).\"Equipment_Owner_ID\"", 
"Expression<()>(template: \"\\\"Equipment\\\"\", bindings: []).\"Facility_ID\"", 
"Expression<()>(template: \"\\\"Equipment\\\"\", bindings: []).\"Location_Details\"", 
"Expression<()>(template: \"\\\"Equipment\\\"\", bindings: []).\"Maintenance_Peridicity\"", 
"Expression<()>(template: \"\\\"Equipment\\\"\", bindings: []).\"Manufacturer\"", 
"Expression<()>(template: \"\\\"Equipment\\\"\", bindings: []).\"Model\"", 
"Expression<()>(template: \"\\\"Equipment\\\"\", bindings: []).\"Notes\"", 
"Expression<()>(template: \"\\\"Equipment\\\"\", bindings: []).\"Planned_Maint_Est_Time\"", 
"Expression<()>(template: \"\\\"Equipment\\\"\", bindings: []).\"Purchase_Price\"", 
"Expression<()>(template: \"\\\"Equipment\\\"\", bindings: []).\"Safety_Inspection_Only\"", 
"Expression<()>(template: \"\\\"Equipment\\\"\", bindings: []).\"Serial\"", 
"Expression<()>(template: \"\\\"Equipment\\\"\", bindings: []).\"Status\"", 
"Expression<()>(template: \"\\\"Facilities\\\"\", bindings: []).\"Facility_ID\"", 
"Expression<()>(template: \"\\\"Facilities\\\"\", bindings: []).\"Facility_Name\"", 
"Expression<()>(template: \"\\\"PreUsers\\\"\", bindings: []).\"Client_ID\"", 
"Expression<()>(template: \"\\\"PreUsers\\\"\", bindings: []).\"Client_Identify\"", 
"Expression<()>(template: \"\\\"PreUsers\\\"\", bindings: []).\"Company_ID\"", 
"Expression<()>(template: \"\\\"PreUsers\\\"\", bindings: []).\"Date_LastLogin\"", 
"Expression<()>(template: \"\\\"PreUsers\\\"\", bindings: []).\"First_Name\"", 
"Expression<()>(template: \"\\\"PreUsers\\\"\", bindings: []).\"Last_Name\"", 
"Expression<()>(template: \"\\\"PreUsers\\\"\", bindings: []).\"Login\"", 
"Expression<()>(template: \"\\\"PreUsers\\\"\", bindings: []).\"Worker_ID\""
]: 

在列表中我可以看到字段:

[...]
"Expression<()>(template: \"\\\"Facilities\\\"\", bindings: []).\"Facility_ID\"", 
"Expression<()>(template: \"\\\"Facilities\\\"\", bindings: []).\"Facility_Name\"",
[...]

但我仍然得到致命的错误。

我可以访问Equipment表中的所有字段,但不能访问连接表中的字段。

生成的SQL似乎是正确的:

print(ext_equipment.asSQL())

返回:

SELECT * FROM "Equipment" INNER JOIN "EquipmentTypes" ON ("EquipmentTypes"."EquipmentType_ID" = "Equipment"."EquipmentType_ID") INNER JOIN "Facilities" ON ("Facilities"."Facility_ID" = "Equipment"."Facility_ID") INNER JOIN "PreUsers" ON ("PreUsers"."Worker_ID" = "Equipment"."Equipment_Owner_ID") WHERE ("Status" >= 0) ORDER BY "Model"

非常感谢任何帮助

0 个答案:

没有答案