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

Commit a6bc9064 by Sascha Depold

fixed multiple primary keys in sqlite => many-to-many associations are now correctly working *meep*

1 parent e5c824db
Showing with 29 additions and 8 deletions
......@@ -25,15 +25,21 @@ module.exports = (function() {
createTableQuery: function(tableName, attributes, options) {
options = options || {}
var query = "CREATE TABLE IF NOT EXISTS <%= table %> (<%= attributes%>)"
, attrStr = []
var query = "CREATE TABLE IF NOT EXISTS <%= table %> (<%= attributes%>)"
, primaryKeys = []
, needsMultiplePrimaryKeys = (Utils._.values(attributes).filter(function(definition) {
return Utils._.includes(definition, 'PRIMARY KEY')
}).length > 1)
, attrStr = []
for (var attr in attributes) {
if (attributes.hasOwnProperty(attr)) {
var dataType = attributes[attr]
if (Utils._.includes(dataType, 'PRIMARY KEY')) {
attrStr.push(Utils.addTicks(attr) + " " + dataType)
if (Utils._.includes(dataType, 'PRIMARY KEY') && needsMultiplePrimaryKeys) {
primaryKeys.push(attr)
attrStr.push(Utils.addTicks(attr) + " " + dataType.replace(/PRIMARY KEY/, 'NOT NULL'))
} else {
attrStr.push(Utils.addTicks(attr) + " " + dataType)
}
......@@ -45,6 +51,11 @@ module.exports = (function() {
attributes: attrStr.join(", "),
charset: (options.charset ? "DEFAULT CHARSET=" + options.charset : "")
}
, pkString = primaryKeys.map(function(pk) { return Utils.addTicks(pk) }).join(", ")
if (pkString.length > 0) {
values.attributes += ", PRIMARY KEY (" + pkString + ")"
}
return Utils._.template(query)(values).trim() + ";"
},
......@@ -112,16 +123,26 @@ module.exports = (function() {
var template = "<%= type %>"
, replacements = { type: dataType.type }
if(dataType.hasOwnProperty('allowNull') && !dataType.allowNull && !dataType.primaryKey)
if (dataType.hasOwnProperty('allowNull') && !dataType.allowNull && !dataType.primaryKey) {
template += " NOT NULL"
}
if(dataType.defaultValue != undefined) {
template += " DEFAULT <%= defaultValue %>"
replacements.defaultValue = Utils.escape(dataType.defaultValue)
}
if(dataType.unique) template += " UNIQUE"
if(dataType.primaryKey) template += " PRIMARY KEY"
if (dataType.unique) {
template += " UNIQUE"
}
if (dataType.primaryKey) {
template += " PRIMARY KEY"
if (dataType.autoIncrement) {
template += ' AUTOINCREMENT'
}
}
result[name] = Utils._.template(template)(replacements)
} else {
......@@ -139,7 +160,7 @@ module.exports = (function() {
if (factory.attributes.hasOwnProperty(name)) {
var definition = factory.attributes[name]
if (definition && (definition.indexOf('INTEGER PRIMARY KEY') === 0)) {
if (definition && (definition.indexOf('INTEGER PRIMARY KEY AUTOINCREMENT') === 0)) {
fields.push(name)
}
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!