'Is there a way to lock table name case in sequelize.js migration
I was moving a project from windows machine to linux machine and I noticed that names of the tables created by Sequelize migrations are named differently between machines. When created by following script:
migration.createTable("Interests", {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER
},
name: {
type: DataTypes.STRING
},
createdAt: {
type: DataTypes.DATE,
allowNull: false
},
updatedAt: {
type: DataTypes.DATE,
allowNull: false
}
}).done(done);
On Windows table is named interests all lowercase but on Linux it is Interests with first letter capitalized.
It there any way to make these work consistently on all platforms?
Solution 1:[1]
Go to your model and pass a parameter underscored and set it to true.
This will force your table to have lowercase letters in both windows and Linux. Do this for all models.
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Payment extends Model {
static associate(models) {
models.Payment.belongsTo(models.Booking, {
foreignKey: 'booking_id',
});
}
}
Payment.init({
amount: DataTypes.INTEGER,
booking_id: DataTypes.INTEGER,
date: DataTypes.DATE
}, {
sequelize,
modelName: 'Payment',
timestamps: false,
name: {
singular: 'payment',
plural: 'payments'
},
underscored: true
});
return Payment;
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rafat Rashid Rahi |
