0

I have a query with a column that should be aliased

When I try to alias the column, return nil.

this is my code :

 func show() {
        let cat_table = Table("cat")
        let id = Expression<Int>("id")
        let cat_text = Expression<String>("text")
        
        do {
            let resultSet =  try db.prepare(cat_table
                                                .select(id,
                                                 cat_text,
                                                 cat_table[id].alias(name:"C_ID")))
                                                .map({Model(row: $0)})
            for res in resultSet {
                print(" Name: \(res.c_id) -- ID: \(res.text) \n")
            }
            
        } catch {
            fatalError(error.localizedDescription)
        }
      
    }

my model :

class Model {
    let id: Int
    let text: String
    let c_id: Int?
    
    init(row: Row) {
        do {
            id = try row.get(Expression<Int>("id"))
            text = try row.get(Expression<String>("text"))
            c_id = try? row.get(Expression<Int>("C_ID")) //If i remove the ? it will crash
        } catch { fatalError() }
    }
    
    init() {
        id = 1
        text = ""
        c_id = 1
    }
  
}

and my table scheme is: cat (id INT, text: TEXT)

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    You already had the same topic a couple of days ago. What's new? Why don't you edit the initial one? – El Tomato Aug 22 '21 at 07:50
  • You were told last time that you cannot do it, right? So what is it? You have a different theory? Or you don't take 'no' as an answer? – El Tomato Aug 22 '21 at 08:25

0 Answers0