我有以下型号
(迁移) transaction.js
'use strict';
module.exports = {
up: function(queryInterface, Sequelize) {
return queryInterface.createTable('transactions', {
date: {
type: Sequelize.DATEONLY
},
transNo: {
type: Sequelize.INTEGER,
allowNull: false,
autoIncrement: true,
primaryKey: true,
},
accNo: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: function(queryInterface, Sequelize) {
return queryInterface.dropTable('transactions');
}
};
(模型) transaction.js
'use strict';
const credit = require('./credit.js');
module.exports = function(sequelize, DataTypes) {
var transaction = sequelize.define('transaction', {
date: DataTypes.DATEONLY,
transNo: DataTypes.INTEGER,
accNo: DataTypes.INTEGER
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
transaction.hasOne(models.credit, {foreignKey: 'transNo'});
}
}
});
return transaction;
};
(模特) credit.js
module.exports = function(sequelize, DataTypes) {
var credit = sequelize.define('credit', {
transNo: DataTypes.INTEGER,
creNo: DataTypes.INTEGER
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
}
}
});
return credit;
};
(迁移) credit.js
` module.exports = {
up: function(queryInterface, Sequelize) {
return queryInterface.createTable('credits', {
transNo: {
type: Sequelize.INTEGER
},
creNo: {
type: Sequelize.INTEGER,
autoIncrement: true,
allowNull: false,
primaryKey: true
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: function(queryInterface, Sequelize) {
return queryInterface.dropTable('credits');
}
};`
它仍然使用id列
进行查询执行(默认):SELECT transaction
。id
,transaction
。date
,transaction
。accNo
,..... ....(等等)。
如何禁止查询id列?
答案 0 :(得分:-1)
您只需将另一列设置为主键,默认情况下不会生成id
列。
module.exports = function(sequelize, DataTypes) {
var credit = sequelize.define('credit', {
transNo: DataTypes.INTEGER,
{
creNo: DataTypes.INTEGER,
primaryKey: true //define a primaryKey
}
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
}
}
});
return credit;
};