我一直在关注Ben Awad在YouTube上的教程。具体来说,我正在使用ppl = True
num = 3
string = f'I am {num:.2f}' if ppl else f'I am {num}'
print(string)
#if ppl False
#=> i am 3
#if ppl True
#=> i am 3.00
来建立他概述的PostgreSQL数据库。
我想用驼峰大小写来定义我的字段,并像他在教程中所做的那样将它们转换成蛇形大小写,特别是在sequelize
here中也概述了docs。但是,当我执行相同的操作时,我的表字段不会转换为蛇形。
这是我使用的代码的MWE:
models / index.js
sequelize
从上面的文件中可以理解,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;
应该将所有表字段都转换为蛇形,如上面的教程视频所述。
models / appointment.js
define: {underscored: true}
index.js
export default (sequelize, DataTypes) => {
const Appointment = sequelize.define("appointment",
{
status: {
type: DataTypes.STRING
},
appointmentStart: {
type: DataTypes.DATE,
},
appointmentEnd: {
type: DataTypes.DATE,
},
},
);
return Appointment;
};
上述MWE的输出SQL如下:
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)
});
可以看出,它不会将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;
之类的字段转换为appointmentStart
。
我还尝试过在单个模型上声明appointment_start
,例如:
underscored: true
这也行不通。
我觉得我可能正在丢失一些明显的东西,或者存在根本性的误会。
答案 0 :(得分:3)
您使用的是哪个版本的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',
},