I created a table in PostgreSQL using knex:
await knex.schema
.withSchema('public')
.createTable('question', (table) => {
table.increments('id').primary();
table.text('title').notNullable().index();
table.text('description').notNullable();
})
Question Table:
| id | title | description |
| -- | ----- | ----------- |
| 0 | abc | hello world |
| 1 | def | Sleep World |
| 2 | ghi | dream world |
Question model:
export interface Question {
title: string;
desciption: string;
}
Using Objection JS I created a query to get the questions:
getQuestions() {
return Question.query().select('title', 'description');
}
Then, I try to assign Question[]
type to the result:
const q: Question[] = await getQuestions();
The error says that I cannot assign the type because:
Type 'Model' is missing the following properties from type 'Question': title, desciption
When I print out the output (without assigning type), the results were wrapped in the table name:
[
Question { title: 'abc', description: 'hello world' },
Question { title: 'def', description: 'Sleep World' },
Question { title: 'ghi', description: 'dream world' },
]
Why is the results wrapped in table name? I heard that they were normal, but it never happened to other nearly identical code.
Why can't I assign types to the results?