作者:xiao15387977702 | 来源:互联网 | 2022-12-07 14:44
I've been following Ben Awad's tutorials on YouTube. Specifically I'm using sequelize
to set up a PostgreSQL database as he outlines.
I want to define my fields in camel case and have them converted to snake case as he does in his tutorials, specifically here, also outlined in the sequelize
docs. However when I do the same my table fields are not converted to snake case.
Here's a MWE of the code I'm using:
models/index.js
import Sequelize from 'sequelize';
var sequelize = new Sequelize('dms', 'postgres', 'postgres', {
dialect: 'postgres',
define: {
underscored: true,
freezeTableName: true,
},
});
const models = {
Appointments: sequelize.import('./appointment'),
};
Object.keys(models).forEach(modelName => {
if (models[modelName].associate) {
models[modelName].associate(models);
}
});
models.sequelize = sequelize;
models.Sequelize = Sequelize;
export default models;
From the file above it is my understanding that define: {underscored: true}
should convert all table fields to snake case, as stated in his tutorial video above.
models/appointment.js
export default (sequelize, DataTypes) => {
const Appointment = sequelize.define("appointment",
{
status: {
type: DataTypes.STRING
},
appointmentStart: {
type: DataTypes.DATE,
},
appointmentEnd: {
type: DataTypes.DATE,
},
},
);
return Appointment;
};
index.js
import { ApolloServer, gql} from 'apollo-server';
import typeDefs from './schema';
import resolvers from './resolvers';
import models from './models';
const PORT = 8080;
const graphqlEndpoint = '/graphql';
const server = new ApolloServer({
typeDefs,
resolvers,
graphqlPath: graphqlEndpoint
});
models.sequelize.sync({force:true}).then(() => {
server.listen(PORT)
});
The output SQL from the above MWE is as follows:
Executing (default): DROP TABLE IF EXISTS "appointment" CASCADE;
Executing (default): DROP TABLE IF EXISTS "appointment" CASCADE;
Executing (default): CREATE TABLE IF NOT EXISTS "appointment" ("id" SERIAL , "status" VARCHAR(255), "appointmentStart" TIMESTAMP WITH TIME ZONE, "appointmentEnd" TIMESTAMP WITH TIME ZONE, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL, "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL, PRIMARY KEY ("id"));
Executing (default): SELECT i.relname AS name, ix.indisprimary AS primary, ix.indisunique AS unique, ix.indkey AS indkey, array_agg(a.attnum) as column_indexes, array_agg(a.attname) AS column_names, pg_get_indexdef(ix.indexrelid) AS definition FROM pg_class t, pg_class i, pg_index ix, pg_attribute a WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND t.relkind = 'r' and t.relname = 'appointment' GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname;
As can be seen, it does not convert fields such as appointmentStart
to appointment_start
.
I have also tried declaring underscored: true
on the indiviudal models, such as the following:
export default (sequelize, DataTypes) => {
const Appointment = sequelize.define("appointment",
{
status: {
type: DataTypes.STRING
},
appointmentStart: {
type: DataTypes.DATE,
},
appointmentEnd: {
type: DataTypes.DATE,
},
}, { underscored: true }
);
return Appointment;
};
This too does not work.
我觉得我可能会遗漏一些明显的东西,或者有根本的误解。
1> 小智..:
您正在使用哪个版本的Sequelize?我猜是v4或更早的版本;该underscored
选项仅适用于v5或更高版本上的属性/列名称。
在Sequelize v4中,唯一underscored: true
会影响的属性名称timestamps: true
是使用Sequelize时创建的时间戳属性。(即createdAt
=> created_at
,updatedAt
=> updated_at
等),
在您声明的模型列中sequelize.define()
,不要将其转换为蛇形,您需要field
在每个驼峰式列名称上显式设置属性,以实现此目的。对于appointmentStart
上面的列,它看起来像:
...
appointmentStart: {
type: DataTypes.DATE,
field: 'appointment_start',
},