Create a model in the database. Should not be called from a child model.
()
| 152 | |
| 153 | /** Create a model in the database. Should not be called from a child model. */ |
| 154 | static async createTable() { |
| 155 | if (this._isCreatedInDatabase) { |
| 156 | throw new Error("This model has already been initialized."); |
| 157 | } |
| 158 | |
| 159 | const createQuery = this._options.queryBuilder |
| 160 | .queryForSchema(this) |
| 161 | .table(this.table) |
| 162 | .createTable( |
| 163 | this.formatFieldToDatabase(this.fields) as ModelFields, |
| 164 | this.formatFieldToDatabase(this.defaults) as ModelDefaults, |
| 165 | { |
| 166 | withTimestamps: this.timestamps, |
| 167 | ifNotExists: true, |
| 168 | }, |
| 169 | ) |
| 170 | .toDescription(); |
| 171 | |
| 172 | await this._options.database.query(createQuery); |
| 173 | |
| 174 | this._isCreatedInDatabase = true; |
| 175 | } |
| 176 | |
| 177 | /** Manually find the primary field by going through the schema fields. */ |
| 178 | private static _findPrimaryField(): FieldOptions { |
no test coverage detected