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

Commit f66ef665 by Mick Hansen

Merge pull request #2651 from sequelize/hotfix/primary-key-allow-null

Fix allowNull in combination with primaryKey
2 parents 7e9b334f 977d6262
...@@ -38,8 +38,8 @@ module.exports = (function() { ...@@ -38,8 +38,8 @@ module.exports = (function() {
var query = 'CREATE TABLE IF NOT EXISTS <%= table %> (<%= attributes%>)' var query = 'CREATE TABLE IF NOT EXISTS <%= table %> (<%= attributes%>)'
, primaryKeys = [] , primaryKeys = []
, needsMultiplePrimaryKeys = (Utils._.values(attributes).filter(function(definition) { , needsMultiplePrimaryKeys = (Utils._.values(attributes).filter(function(definition) {
return Utils._.includes(definition, 'PRIMARY KEY'); return Utils._.includes(definition, 'PRIMARY KEY');
}).length > 1) }).length > 1)
, attrStr = [] , attrStr = []
, modifierLastIndex = -1; , modifierLastIndex = -1;
...@@ -276,7 +276,7 @@ module.exports = (function() { ...@@ -276,7 +276,7 @@ module.exports = (function() {
} }
} }
if (dataType.hasOwnProperty('allowNull') && !dataType.allowNull && !dataType.primaryKey) { if (dataType.hasOwnProperty('allowNull') && !dataType.allowNull) {
template += " NOT NULL"; template += " NOT NULL";
} }
......
...@@ -134,6 +134,31 @@ describe(Support.getTestDialectTeaser("Model"), function () { ...@@ -134,6 +134,31 @@ describe(Support.getTestDialectTeaser("Model"), function () {
]); ]);
}); });
describe('primaryKey', function () {
describe('in combination with allowNull', function () {
beforeEach(function () {
this.ModelUnderTest = this.sequelize.define('ModelUnderTest', {
identifier: {
primaryKey: true,
type: Sequelize.STRING,
allowNull: false
}
});
return this.ModelUnderTest.sync({ force: true });
});
it('sets the column to not allow null', function () {
return this
.ModelUnderTest
.describe()
.then(function (fields) {
expect(fields.identifier).to.include({ allowNull: false });
});
});
});
});
describe('field and attribute name is the same', function () { describe('field and attribute name is the same', function () {
beforeEach(function () { beforeEach(function () {
return this.Comment.bulkCreate([ return this.Comment.bulkCreate([
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!