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

Commit 0fb0f54e by Sascha Depold

dont add id as primary key if primaryKeys were defined for the model

1 parent 25491005
......@@ -21,6 +21,9 @@ ModelDefinition.prototype.addDefaultAttributes = function() {
var defaultAttributes = {id: {type: DataTypes.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true}}
, self = this
if(Utils._.keys(this.primaryKeys).length > 0)
defaultAttributes = {}
if(this.options.timestamps) {
defaultAttributes[this.options.camelcase ? 'created_at' : 'createdAt'] = {type: DataTypes.DATE, allowNull: false}
defaultAttributes[this.options.camelcase ? 'updated_at' : 'updatedAt'] = {type: DataTypes.DATE, allowNull: false}
......@@ -74,6 +77,9 @@ ModelDefinition.prototype.find = function(options) {
// options is not a hash but an id
if(typeof options == 'number')
options = {where: options}
else {
// if(arguments.length == this.primaryKeys.length)
}
options.limit = 1
......@@ -94,4 +100,15 @@ ModelDefinition.prototype.create = function(values) {
return this.build(values).save()
}
ModelDefinition.prototype.__defineGetter__('primaryKeys', function() {
var result = {}
Utils._.each(this.attributes, function(dataTypeString, attributeName) {
if(dataTypeString.indexOf('PRIMARY KEY') > -1)
result[attributeName] = dataTypeString
})
return result
})
Utils._.map(require("./association-mixin").classMethods, function(fct, name) { ModelDefinition.prototype[name] = fct })
\ No newline at end of file
......@@ -31,7 +31,14 @@ module.exports = {
var User = sequelize.define('User' + parseInt(Math.random() * 999999999), {
username: {type: Sequelize.STRING, primaryKey: true}
}, { timestamps: false })
assert.eql(User.attributes, {username:"VARCHAR(255) PRIMARY KEY",id:"INT NOT NULL auto_increment PRIMARY KEY"})
assert.eql(User.attributes, {username:"VARCHAR(255) PRIMARY KEY"})
},
'primaryKeys should be correctly determined': function() {
var User = sequelize.define('User' + parseInt(Math.random() * 999999999), {
foo: {type: Sequelize.STRING, primaryKey: true},
bar: Sequelize.STRING
})
assert.eql(User.primaryKeys, {"foo":"VARCHAR(255) PRIMARY KEY"})
},
'it should add updatedAt and createdAt if timestamps is undefined or true': function() {
var User1 = sequelize.define('User' + parseInt(Math.random() * 999999999), {})
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!