我有一个模型创建我的Country
表
var Country = db.define(
'country',
{
name: {
type: Sequelize.STRING,
allowNull: false,
},
GFI: {
type: Sequelize.DECIMAL,
validate: {
max: 10,
min: 0,
},
},
flagUrl: {
type: Sequelize.STRING,
defaultValue: 'https://upload.wikimedia.org/wikipedia/en/a/ae/Flag_of_the_United_Kingdom.svg',
},
},
{}
);
制作定义'飞机'
的模型var Country = require('./country');
var Aircraft = db.define(
'aircraft',
{
make: {
type: Sequelize.STRING,
allowNull: false,
},
model: {
type: Sequelize.STRING,
allowNull: false,
},
year: {
type: Sequelize.STRING,
validate: {
isAfter: '1903-12-31',
},
},
type: {
type: Sequelize.STRING,
validate: {
isIn: ['Attack', 'Bomber', 'Versatile', 'Transport', 'Reconoissance', 'Rescue'],
},
},
cost: {
type: Sequelize.DECIMAL(7, 2),
},
imageUrl: {
defaultValue:
'http://media.gettyimages.com/photos/the-wright-brothers-give-a-demonstration-of-their-wright-model-a-picture-id530836658?k=6&m=530836658&s=612x612&w=0&h=66RNNerGkWA4C_784UK1-bXRret0CHY8QDl_HRYSY6I=',
},
description: {
type: Sequelize.TEXT,
},
},
{}
);
我创建了这样的关联:
Aircraft.belongsTo(Country);
Country.hasMany(Aircraft);
我的飞机必须在创作时分配到一个国家。
我知道一个钩子可能是合适的:
Aircraft.hook('beforeCreate', aircraft => {
aircraft.Country.name;
});
但我不确定,任何帮助都会受到赞赏!