Skip to content

Commit

Permalink
Merge pull request #673 from sevastos/value-omittion-of-autoincrement…
Browse files Browse the repository at this point in the history
…al-numerics

BigInt/Datatypes enhancements
  • Loading branch information
durango committed Jun 6, 2013
2 parents a875c64 + 08446fd commit 7df9fcb
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/associations/belongs-to.js
Expand Up @@ -24,7 +24,7 @@ module.exports = (function() {
var newAttributes = {}

this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.target.tableName) + "Id", this.source.options.underscored)
newAttributes[this.identifier] = { type: DataTypes.INTEGER }
newAttributes[this.identifier] = { type: this.options.keyType || DataTypes.INTEGER }
Helpers.addForeignKeyConstraints(newAttributes[this.identifier], this.target, this.source, this.options)
Utils._.defaults(this.source.rawAttributes, newAttributes)

Expand Down
7 changes: 4 additions & 3 deletions lib/associations/has-many.js
Expand Up @@ -51,8 +51,9 @@ module.exports = (function() {

// define a new model, which connects the models
var combinedTableAttributes = {}
combinedTableAttributes[this.identifier] = {type:DataTypes.INTEGER, primaryKey: true}
combinedTableAttributes[this.foreignIdentifier] = {type:DataTypes.INTEGER, primaryKey: true}
var keyType = this.options.keyType || DataTypes.INTEGER
combinedTableAttributes[this.identifier] = {type: keyType, primaryKey: true}
combinedTableAttributes[this.foreignIdentifier] = {type: keyType, primaryKey: true}

this.connectorDAO = this.source.daoFactoryManager.sequelize.define(this.combinedName, combinedTableAttributes, this.options)

Expand All @@ -65,7 +66,7 @@ module.exports = (function() {
}
} else {
var newAttributes = {}
newAttributes[this.identifier] = { type: DataTypes.INTEGER }
newAttributes[this.identifier] = { type: this.options.keyType || DataTypes.INTEGER }
Helpers.addForeignKeyConstraints(newAttributes[this.identifier], this.source, this.target, this.options)
Utils._.defaults(this.target.rawAttributes, newAttributes)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/associations/has-one.js
Expand Up @@ -29,7 +29,7 @@ module.exports = (function() {
var newAttributes = {}

this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.source.tableName) + "Id", this.options.underscored)
newAttributes[this.identifier] = { type: DataTypes.INTEGER }
newAttributes[this.identifier] = { type: this.options.keyType || DataTypes.INTEGER }
Helpers.addForeignKeyConstraints(newAttributes[this.identifier], this.source, this.target, this.options)
Utils._.defaults(this.target.rawAttributes, newAttributes)

Expand Down
3 changes: 2 additions & 1 deletion lib/dialects/postgres/query-generator.js
Expand Up @@ -552,7 +552,7 @@ module.exports = (function() {
}

if (dataType.autoIncrement) {
template +=" SERIAL"
template += " SERIAL"
}

if (dataType.defaultValue !== undefined) {
Expand Down Expand Up @@ -773,6 +773,7 @@ module.exports = (function() {
Utils._.forEach(attrValueHash, function(value, key, hash) {
if (tables[tableName] && tables[tableName][key]) {
switch (tables[tableName][key]) {
case 'bigserial':
case 'serial':
delete hash[key]
returning.push(key)
Expand Down
25 changes: 25 additions & 0 deletions spec/associations/belongs-to.spec.js
Expand Up @@ -179,4 +179,29 @@ describe(Helpers.getTestDialectTeaser("BelongsTo"), function() {

})

describe("Association options", function() {
it('can specify data type for autogenerated relational keys', function(done) {
var User = this.sequelize.define('UserXYZ', { username: Sequelize.STRING })
, dataTypes = [Sequelize.INTEGER, Sequelize.BIGINT, Sequelize.STRING]
, self = this

dataTypes.forEach(function(dataType) {
var tableName = 'TaskXYZ_' + dataType.toString()
, Task = self.sequelize.define(tableName, { title: Sequelize.STRING })

Task.belongsTo(User, { foreignKey: 'userId', keyType: dataType })

self.sequelize.sync({ force: true }).success(function() {
expect(Task.rawAttributes.userId.type.toString())
.toEqual(dataType.toString())

dataTypes.splice(dataTypes.indexOf(dataType), 1)
if (!dataTypes.length) {
done()
}
})
})
})
})

})
26 changes: 26 additions & 0 deletions spec/associations/has-many.spec.js
Expand Up @@ -455,4 +455,30 @@ describe(Helpers.getTestDialectTeaser("HasMany"), function() {
})

})

describe("Association options", function() {
it('can specify data type for autogenerated relational keys', function(done) {
var User = this.sequelize.define('UserXYZ', { username: Sequelize.STRING })
, dataTypes = [Sequelize.INTEGER, Sequelize.BIGINT, Sequelize.STRING]
, self = this

dataTypes.forEach(function(dataType) {
var tableName = 'TaskXYZ_' + dataType.toString()
, Task = self.sequelize.define(tableName, { title: Sequelize.STRING })

User.hasMany(Task, { foreignKey: 'userId', keyType: dataType })

self.sequelize.sync({ force: true }).success(function() {
expect(Task.rawAttributes.userId.type.toString())
.toEqual(dataType.toString())

dataTypes.splice(dataTypes.indexOf(dataType), 1)
if (!dataTypes.length) {
done()
}
})
})
})
})

})
25 changes: 25 additions & 0 deletions spec/associations/has-one.spec.js
Expand Up @@ -179,4 +179,29 @@ describe(Helpers.getTestDialectTeaser("HasOne"), function() {

})

describe("Association options", function() {
it('can specify data type for autogenerated relational keys', function(done) {
var User = this.sequelize.define('UserXYZ', { username: Sequelize.STRING })
, dataTypes = [Sequelize.INTEGER, Sequelize.BIGINT, Sequelize.STRING]
, self = this

dataTypes.forEach(function(dataType) {
var tableName = 'TaskXYZ_' + dataType.toString()
, Task = self.sequelize.define(tableName, { title: Sequelize.STRING })

User.hasOne(Task, { foreignKey: 'userId', keyType: dataType })

self.sequelize.sync({ force: true }).success(function() {
expect(Task.rawAttributes.userId.type.toString())
.toEqual(dataType.toString())

dataTypes.splice(dataTypes.indexOf(dataType), 1)
if (!dataTypes.length) {
done()
}
})
})
})
})

})
30 changes: 29 additions & 1 deletion spec/dao-factory.spec.js
Expand Up @@ -399,7 +399,7 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
})
})

it('should only store the values passed in the witelist', function(done) {
it('should only store the values passed in the whitelist', function(done) {
var self = this
, data = { username: 'Peter', secretValue: '42' }

Expand All @@ -426,6 +426,34 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
})
})

it('can omitt autoincremental columns', function(done) {
var self = this
, data = { title: 'Iliad' }
, dataTypes = [Sequelize.INTEGER, Sequelize.BIGINT]

dataTypes.forEach(function(dataType, index) {
var Book = self.sequelize.define('Book'+index, {
id: { type: dataType, primaryKey: true, autoIncrement: true },
title: Sequelize.TEXT
})
Book.sync({ force: true }).success(function() {
Book
.create(data)
.success(function(book) {
expect(book.title).toEqual(data.title)
expect(book.author).toEqual(data.author)
expect(Book.rawAttributes.id.type.toString())
.toEqual(dataTypes[index].toString())

Book.drop()
if (index >= dataTypes.length - 1) {
done()
}
})
})
})
})

it('saves data with single quote', function(done) {
var quote = "single'quote"
, self = this
Expand Down

0 comments on commit 7df9fcb

Please sign in to comment.