如何在Sequelize中创建一个字段?

时间:2017-07-30 10:30:35

标签: mysql node.js postgresql sequelize.js

让我们说我想要制作续集中所需的名字字段,在猫鼬中我只能说要求:在该字段上是真的但是我如何在Sequelize中这样做?

2 个答案:

答案 0 :(得分:12)

如果你正在使用Sequelize,那么mongoose上的allowNull: false相当于const YourTable = sequelize.define('your_table', { firstname: { type: Sequelize.STRING, // This will require the firstname be present allowNull: false, // If you want to also have a length restriction, add the next line len: [2,50], // only allow values with length between 2 and 50 // That is 'Al' will be accepted and so will 'Rigoberto Fernando Luis María'. // But '' or 'J' won't be good enough // If you use sequelize transforms, this will remove spaces on both ends // of the string also trim: true, }, // All the other fields (columns) }); 。你可以按照以下方式写一些东西:

func recordEvent(_ category: String, action: String, label: String?, value: Int?) {

    guard let builder = GAIDictionaryBuilder.createEvent(
        withCategory: category,
        action: action,
        label: label,
        value: value as NSNumber?)
    else { return }

    GAI.sharedInstance().defaultTracker.send(builder.build() as [NSObject: AnyObject])
}

参考文献:

答案 1 :(得分:3)

要回答Dmitry问题:您可以在字段定义下定义自定义验证错误:

foo: {
  type: Sequelize.STRING,
  allowNull: false,
  validate: {
    notNull: { msg: "foo is required" },
  },
}

更多信息here