不要怂,就是干,撸起袖子干!

Commit 7d668dda by Jan Aagaard Meier

bug(define) Throw an error when adding a column named id, without marking it as pk. Closes #4139

1 parent a42b38b7
# Next
- [FIXED] Fall back to a default version when parsing the DB version fails [#4368](https://github.com/sequelize/sequelize/issues/4368)
- [FIXED] Fix a bug where passing null as the second parameter to `sequelize.where` would fail [#4334](https://github.com/sequelize/sequelize/issues/4334)
- [FIXED] An error is thrown if a column called `id` is added, but not marked as primary key, and no other pk is present. [#4139](https://github.com/sequelize/sequelize/issues/4139)
# 3.6.0
- [ADDED] Model.findCreateFind: A more performant findOrCreate that will not work under a transaction (atleast not in postgres)
......
'use strict';
/* jshint -W110 */
var Utils = require('./utils')
, Instance = require('./instance')
, Association = require('./associations/base')
......@@ -160,6 +162,11 @@ var addDefaultAttributes = function() {
// Add id if no primary key was manually added to definition
// Can't use this.primaryKeys here, since this function is called before PKs are identified
if (!_.any(this.rawAttributes, 'primaryKey')) {
if ('id' in this.rawAttributes) {
// Something is fishy here!
throw new Error("A column called 'id' was added to the attributes of '" + this.tableName + "' but not marked with 'primaryKey: true'");
}
head = {
id: {
type: new DataTypes.INTEGER(),
......
......@@ -956,8 +956,8 @@ describe(Support.getTestDialectTeaser('Sequelize'), function() {
describe('table', function() {
[
{ id: { type: DataTypes.BIGINT } },
{ id: { type: DataTypes.STRING, allowNull: true } },
{ id: { type: DataTypes.BIGINT, primaryKey: true } },
{ id: { type: DataTypes.STRING, allowNull: true, primaryKey: true } },
{ id: { type: DataTypes.BIGINT, allowNull: false, primaryKey: true, autoIncrement: true } }
].forEach(function(customAttributes) {
......
'use strict';
/* jshint -W030 */
/* jshint -W030, -W110 */
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, DataTypes = require('../../../lib/data-types')
, current = Support.sequelize;
describe(Support.getTestDialectTeaser('Model'), function() {
......@@ -27,5 +28,21 @@ describe(Support.getTestDialectTeaser('Model'), function() {
expect(Model.rawAttributes.created_at).not.to.be.defined;
expect(Model.rawAttributes.updated_at).not.to.be.defined;
});
it('should throw when id is added but not marked as PK', function () {
expect(function () {
current.define('foo', {
id: DataTypes.INTEGER
});
}).to.throw("A column called 'id' was added to the attributes of 'foos' but not marked with 'primaryKey: true'");
expect(function () {
current.define('bar', {
id: {
type: DataTypes.INTEGER
}
});
}).to.throw("A column called 'id' was added to the attributes of 'bars' but not marked with 'primaryKey: true'");
});
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!