You want a column to be called 'id' instead of 'TableID' because you don't have access to the database to rename the field directly
me@jaykilleen.com wrote this over 7 years ago and it was last updated over 7 years ago.
Sometimes you are building an application that sits on top of an existing database. This database might be old and have table and column name syntax that is not easy to understand, other times the owner of that database made the decision to name things poorly and you are not allowed to rename the field in the database.
You are using trails.js with the trailpack-sequelize and you want to now rename that table (model) or column (field) directly in the model definition.
Here is how I got it to work.
Note the use of field
in the schema of each column name. Field refers to what it is called in the database.
/api/models/customer.js
'use strict'
const Model = require('trails/model')
/**
* @module Customer
* @description TODO document Model
*/
module.exports = class Customer extends Model {
static config (app, Sequelize) {
return {
store: 'db',
options: {
schema: 'Sales',
tableName: 'Account',
timestamps: false,
classMethods: {
//If you need associations, put them here
associate: (models) => {
}
}
}
}
}
static schema (app, Sequelize) {
return {
id: {
type: Sequelize.STRING,
allowNull: false,
primaryKey: true,
field: 'CustomerCode'
},
name: {
type: Sequelize.STRING,
field: 'CustomerName',
}
}
}
}
You can now view the SQL string being sent to the database in your logs. Mine looks like
SELECT [Customer].[CustomerCode] AS [id], [Customer].[CustomerName] AS [name] FROM [Sales].[Account] AS [Customer];
You can read about the decision to do this in the Github issue 2209 for the Sequelize library.
Thanks to @jaumard for talking me through this in the trails.js Gitter Room.