helpers.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. var Utils = require("./../utils")
  2. module.exports = {
  3. addForeignKeyConstraints: function(newAttribute, source, target, options) {
  4. // FK constraints are opt-in: users must either rset `foreignKeyConstraints`
  5. // on the association, or request an `onDelete` or `onUpdate` behaviour
  6. if(options.foreignKeyConstraint || options.onDelete || options.onUpdate) {
  7. // Find primary keys: composite keys not supported with this approach
  8. var primaryKeys = Utils._.filter(Utils._.keys(source.rawAttributes), function(key) {
  9. return source.rawAttributes[key].primaryKey
  10. })
  11. if (primaryKeys.length === 1) {
  12. if (!!source.options.schema) {
  13. newAttribute.references = source.daoFactoryManager.sequelize.queryInterface.QueryGenerator.addSchema({
  14. tableName: source.tableName,
  15. options: {
  16. schema: source.options.schema,
  17. schemaDelimiter: source.options.schemaDelimiter
  18. }
  19. })
  20. } else {
  21. newAttribute.references = source.tableName
  22. }
  23. newAttribute.referencesKey = primaryKeys[0]
  24. newAttribute.onDelete = options.onDelete
  25. newAttribute.onUpdate = options.onUpdate
  26. }
  27. }
  28. }
  29. }