sequelize.js includesTo不为null无效

时间:2018-07-25 01:49:58

标签: node.js sequelize.js

我用Node.js构建了一些API系统

还使用sequelize.js(版本4)与MySQL进行通信。

在我的模型中,我定义了两个模型。

[model.js]

//用户模型

int convertMJPGToI420() {

    auto fileSize = filesize(IMG_NAME);

    // load image into memory
    uint8_t* my_img = (uint8_t*) calloc(fileSize, 1);
    std::ifstream fin(IMG_NAME, ios::in | ios::binary);
    fin.read(reinterpret_cast<char*>(my_img), fileSize);

    // exif data offset 
    // This is the size of the exif data
    const int kOff = 4096;

    // 4k image is being sent in
    int benchmark_width_ = 3840;
    int benchmark_height_ = 2160;

    const int kSize = fileSize;

    // align_buffer_page_end is a macro (look at link posted for unit tests above)
    // I'm not sure if the size allocation for these is correct
    // I have tried to model it based off the example
    align_buffer_page_end(orig_pixels, kSize);
    align_buffer_page_end(dst_y_opt, benchmark_width_ * benchmark_height_);
    align_buffer_page_end(dst_u_opt, SUBSAMPLE(benchmark_width_, 2) * 
                                        SUBSAMPLE(benchmark_height_, 2));
    align_buffer_page_end(dst_v_opt, SUBSAMPLE(benchmark_width_, 2) * 
                                        SUBSAMPLE(benchmark_height_, 2));

    // EOI, SOI to make MJPG appear valid
    memset(orig_pixels, 0, kSize);
    orig_pixels[0] = 0xff;
    orig_pixels[1] = 0xd8; // SOI

    memcpy(orig_pixels + 2, my_img, kSize - kOff - 3);

    orig_pixels[kSize - kOff + 0] = 0xff;
    orig_pixels[kSize - kOff + 1] = 0xd9; // EOI

    // using async as this function might be ansynchronous
    std::future<int> ret = std::async(libyuv::MJPGToI420, orig_pixels, kSize, dst_y_opt, benchmark_width_,
                        dst_u_opt, SUBSAMPLE(benchmark_width_, 2),
                        dst_v_opt, SUBSAMPLE(benchmark_width_, 2), 
                        benchmark_width_, benchmark_height_, 
                        benchmark_width_, benchmark_height_);

    ret.wait();

    // ret is always one, which means there was a failure
    if(ret.get() == 0) {
        cout << "return value was zero" << endl;
    } else {
        cout << "return value was one" << endl;
    }

    FILE* file = fopen("/data/dst_u_opt", "wb");
    fwrite(dst_y_opt, 1, SUBSAMPLE(benchmark_width_, 2) * SUBSAMPLE(benchmark_height_, 2) , file);
    fclose(file);

    file = fopen("/data/dst_v_opt", "wb");
    fwrite(dst_y_opt, 1, SUBSAMPLE(benchmark_width_, 2) *  SUBSAMPLE(benchmark_height_, 2), file);
    fclose(file);

    free_aligned_buffer_page_end(dst_y_opt);
    free_aligned_buffer_page_end(dst_u_opt);
    free_aligned_buffer_page_end(dst_v_opt);
    free_aligned_buffer_page_end(orig_pixels);

    return 0;
}

并定义了用于相互连接模型的关联。

export const User = sequelize.define('user', {
    id: {
        type: Sequelize.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    username: {
        type: Sequelize.STRING(30),
        unique: true,
        allowNull: false
    },
    email: {
        type: Sequelize.STRING(50),
        unique: true,
        allowNull: false
    },
    password: {
        type: Sequelize.STRING(30),
        allowNull: false
    },
    profile_image: {
        type: Sequelize.BLOB
    },
    phone: {
        type: Sequelize.STRING(14),
        allowNull: true,
        unique: true
    },
    gender: {
        type: Sequelize.STRING(5),
        allowNull: false,
    },
    is_admin: {
        type: Sequelize.BOOLEAN,
        defaultValue: false
    }
}, {
    freezeTableName: true,
    timestamps: true,
    underscored: true
})

// Image model
export const Image = sequelize.define('image', {
    file: {
        type: Sequelize.BLOB
    },
    location: {
        type: Sequelize.STRING(100)
    },
    caption: {
        type: Sequelize.STRING(100)
    },
}, {
    freezeTableName: true,
    underscored: true,
    timestamps: true
})

在上面的代码中,我定义了Image.belongsTo(User, {foreignKey: 'creator', targetKey: 'username', onDelete: 'CASCADE', allowNull: false}); User.hasMany(Image, {foreignKey: 'creator', allowNull: false}); 以防止null。

但是当我描述图像时,它允许为空。

allowNull: false

如何将mysql> desc image; +------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | file | blob | YES | | NULL | | | location | varchar(100) | YES | | NULL | | | caption | varchar(100) | YES | | NULL | | | created_at | datetime | NO | | NULL | | | updated_at | datetime | NO | | NULL | | | creator | varchar(30) | YES | MUL | NULL | | +------------+--------------+------+-----+---------+----------------+ 设置为not null

2 个答案:

答案 0 :(得分:0)

[已解决]

在我将creator列定义为图片模型之后,

creator: {
    type: Sequelize.STRING(30),
    allowNull: false,
}

它工作正常。

但是,为什么在关联中指定allowNull: false无效?

(之前,我没有定义FK列进行建模,因为关联会自动创建列)

答案 1 :(得分:0)

allowNull 必须嵌套在两个模型上的 foreignKey 对象中才能使其正常工作。

Image.belongsTo(User, {onDelete: 'CASCADE', foreignKey: {allowNull: false}});
User.hasMany(Image, {foreignKey: {allowNull: false}});