Here is my issue:
I wish to delete all rows at row-group-view
if i delete a group from group
or if i delete a view
from view.
Last active
May 7, 2021 21:15
-
-
Save kylefelipe/1e3a23b9ae4298b977252f9bfd648638 to your computer and use it in GitHub Desktop.
Sequelize issue about N:N relations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
module.exports = { | |
up: (queryInterface, Sequelize) => { | |
return queryInterface.createTable('groups', { | |
id: { | |
allowNull: false, | |
autoIncrement: true, | |
primaryKey: true, | |
type: Sequelize.INTEGER | |
}, | |
id_project: { | |
type: Sequelize.INTEGER | |
}, | |
name: { | |
type: Sequelize.STRING | |
}, | |
code: { | |
type: Sequelize.STRING | |
}, | |
}, | |
{ | |
charset: 'utf-8', | |
schema: 'terrama2' | |
}); | |
}, | |
down: (queryInterface, Sequelize) => { | |
return queryInterface.dropTable('groups'); | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
module.exports = { | |
up: (queryInterface, Sequelize) => { | |
return queryInterface.createTable('rel_group_views', { | |
id: { | |
allowNull: false, | |
autoIncrement: true, | |
primaryKey: true, | |
type: Sequelize.INTEGER | |
}, | |
id_view: { | |
type: Sequelize.INTEGER, | |
references: { model: 'views', key: 'id' } | |
}, | |
id_group: { | |
type: Sequelize.INTEGER, | |
references: { model: 'groups', key: 'id' } | |
}, | |
}, { | |
charset: 'utf-8', | |
schema: 'terrama2' | |
}); | |
}, | |
down: (queryInterface, Sequelize) => { | |
return queryInterface.dropTable('rel_group_views', { schema: 'terrama2' }); | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const {DataTypes} = require('sequelize'); | |
module.exports = (sequelize) => { | |
const Group = sequelize.define('groups', { | |
id: { | |
type: DataTypes.UUID, | |
allowNull: false, | |
primaryKey: true, | |
autoIncrement: true | |
}, | |
name: { | |
type: DataTypes.STRING, | |
allowNull: false, | |
comment: "Name of the group" | |
}, | |
code: { | |
type: DataTypes.STRING, | |
allowNull: false, | |
comment: "Code of the group" | |
} | |
}, { | |
schema: 'terrama2', | |
underscored: true, | |
underscoredAll: true, | |
timestamps: false | |
}) | |
Group.associate = function(models) { | |
console.log(models) | |
Group.belongsToMany(models.views, { | |
through: models.rel_group_view, | |
foreignKey: 'idGroup', | |
as: 'relGroupSview', | |
otherKey: 'idView' | |
}); | |
}; | |
return Group; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const allModels = require('./index') | |
module.exports = (sequelize, DataTypes) => { | |
const RelGroupView = sequelize.define('rel_group_view', { | |
id: { | |
type: DataTypes.UUID, | |
allowNull: false, | |
primaryKey: true, | |
autoIncrement: true | |
}, | |
id_group: { | |
type: DataTypes.INTEGER, | |
references: { | |
model: allModels.groups, | |
key: 'id' | |
} | |
}, | |
id_view: { | |
type: DataTypes.INTEGER, | |
references: { | |
model: allModels.view, | |
key: 'id' | |
} | |
}, | |
}, { | |
schema: 'terrama2', | |
underscored: true, | |
underscoredAll: true, | |
timestamps: false | |
}); | |
// RelGroupView.associate = function(models) { | |
// RelGroupView.belongsTo(models.views, { | |
// through: 'views', | |
// onDelete: 'NO ACTION', | |
// foreignKey: 'id_view', | |
// as: 'views', | |
// otherKey: 'id_view' | |
// }); | |
// RelGroupView.belongsTo(models.groups, { | |
// through: 'groups', | |
// onDelete: 'CASCADE', | |
// foreignKey: 'id_group', | |
// as: 'groups', | |
// otherKey: 'id_group' | |
// }); | |
// }; | |
return RelGroupView; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
module.exports = (sequelize, DataTypes) => { | |
const View = sequelize.define('views', { | |
id: { | |
type: DataTypes.UUID, | |
allowNull: false, | |
primaryKey: true, | |
autoIncrement: true, | |
comment: "View identifier" | |
}, | |
name: { | |
type: DataTypes.STRING, | |
allowNull: false, | |
comment: "View name" | |
}, | |
description: { | |
type: DataTypes.TEXT, | |
comment: "View description" | |
}, | |
active: { | |
type: DataTypes.BOOLEAN, | |
allowNull: false, | |
default: true, | |
comment: "It defines view can be used and retrieved. Default is true." | |
}, | |
private: { | |
type: DataTypes.BOOLEAN, | |
allowNull: false, | |
default: false, | |
comment: "It defines if the view is private. Default is false." | |
}, | |
schedule_type: { | |
type: DataTypes.INTEGER, | |
allowNull: true | |
}, | |
source_type: { | |
type: DataTypes.INTEGER, | |
allowNull: false, | |
comment: "It defines the type of data source that create the view. Alert, Analysis, Static Data or Dynamic Data" | |
}, | |
charts: { | |
type: DataTypes.JSONB, | |
allowNull: true, | |
comment: "Charts" | |
} | |
}, { | |
schema: 'terrama2', | |
underscored: true, | |
underscoredAll: true, | |
timestamps: false, | |
}) | |
View.associate = function(models) { | |
View.belongsToMany(models.groups, { | |
through: models.rel_group_view, | |
as: 'relGroupsView', | |
foreignKey: 'idView', | |
otherKey: 'idGroup' | |
}) | |
} | |
return View | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment