Sequelize - 如何在模型定义期间定义ON DELETE操作

时间:2017-01-21 19:16:04

标签: node.js sequelize.js

我想问你是否可以在定义Sequelize模型时设置ON DELETE操作,例如:

sequelize.define('oAuthAccessTokens', {
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    createdAt: Sequelize.DATE,
    updatedAt: Sequelize.DATE,
    accessToken: { type: Sequelize.STRING(256), allowNull: false },
    expires: { type: Sequelize.DATE, allowNull: false },
    scope: Sequelize.STRING(255),
    clientId: {
        type: Sequelize.STRING(80),
        allowNull: false,
        references: {
            model: 'oAuthClients',
            key: "clientId",
            **onDelete: "cascade"** // here is my try
        }
    },
    userId: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
            model: 'oAuthUsers',
            key: "id",
            **onDelete: "cascade"** // here is my try
        }
    }
});

我知道可以将适当的选项传递给hasOne()方法,但是这样的模型定义呢?我希望有一种方法在官方文件中没有提到。

更新

问题现在解决了。 onDelete属性应该在references之外。

sequelize.define('oAuthAccessTokens', {
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    createdAt: Sequelize.DATE,
    updatedAt: Sequelize.DATE,
    accessToken: { type: Sequelize.STRING(256), allowNull: false },
    expires: { type: Sequelize.DATE, allowNull: false },
    scope: Sequelize.STRING(255),
    clientId: {
        type: Sequelize.STRING(80),
        allowNull: false,
        onDelete: "cascade",
        references: {
            model: 'oAuthClients',
            key: "clientId",
        }
    },
    userId: {
        type: Sequelize.INTEGER,
        onDelete: "cascade",
        allowNull: false,
        references: {
            model: 'oAuthUsers',
            key: "id"
        }
    }
});

1 个答案:

答案 0 :(得分:0)

问题已解决。 #[test] fn something_does_something() { let something = Something { inner: SomethingElse { a: 1 }, }; assert_eq!(something.do_something(), SomethingElse { a: 1 }); } 属性应该在onDelete属性之外。

references