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

Commit de9c66a3 by Ben Leith Committed by GitHub

feat(belongs-to-many): allow creation of paranoid join tables (#12088)

1 parent d16de58f
...@@ -145,7 +145,7 @@ class BelongsToMany extends Association { ...@@ -145,7 +145,7 @@ class BelongsToMany extends Association {
this.through.model = this.sequelize.define(this.through.model, {}, Object.assign(this.options, { this.through.model = this.sequelize.define(this.through.model, {}, Object.assign(this.options, {
tableName: this.through.model, tableName: this.through.model,
indexes: [], //we don't want indexes here (as referenced in #2416) indexes: [], //we don't want indexes here (as referenced in #2416)
paranoid: false, // A paranoid join table does not make sense paranoid: this.through.paranoid ? this.through.paranoid : false, // Default to non-paranoid join (referenced in #11991)
validate: {} // Don't propagate model-level validations validate: {} // Don't propagate model-level validations
})); }));
} else { } else {
......
...@@ -2285,6 +2285,36 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), () => { ...@@ -2285,6 +2285,36 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), () => {
expect(association.through.model.options.paranoid).not.to.be.ok; expect(association.through.model.options.paranoid).not.to.be.ok;
}); });
}); });
it('should allow creation of a paranoid join table', () => {
const paranoidSequelize = Support.createSequelizeInstance({
define: {
paranoid: true
}
}),
ParanoidUser = paranoidSequelize.define('ParanoidUser', {}),
ParanoidTask = paranoidSequelize.define('ParanoidTask', {});
ParanoidUser.belongsToMany(ParanoidTask, {
through: {
model: 'UserTasks',
paranoid: true
}
});
ParanoidTask.belongsToMany(ParanoidUser, {
through: {
model: 'UserTasks',
paranoid: true
}
});
expect(ParanoidUser.options.paranoid).to.be.ok;
expect(ParanoidTask.options.paranoid).to.be.ok;
_.forEach(ParanoidUser.associations, association => {
expect(association.through.model.options.paranoid).to.be.ok;
});
});
}); });
describe('foreign keys', () => { describe('foreign keys', () => {
......
...@@ -21,8 +21,15 @@ import { Association, AssociationScope, ForeignKeyOptions, ManyToManyOptions, Mu ...@@ -21,8 +21,15 @@ import { Association, AssociationScope, ForeignKeyOptions, ManyToManyOptions, Mu
export interface ThroughOptions { export interface ThroughOptions {
/** /**
* The model used to join both sides of the N:M association. * The model used to join both sides of the N:M association.
* Can be a string if you want the model to be generated by sequelize.
*/ */
model: typeof Model; model: typeof Model | string;
/**
* If true the generated join table will be paranoid
* @default false
*/
paranoid?: boolean;
/** /**
* A key/value set that will be used for association create and find defaults on the through model. * A key/value set that will be used for association create and find defaults on the through model.
......
...@@ -115,3 +115,24 @@ const someInstance = new SomeModel() ...@@ -115,3 +115,24 @@ const someInstance = new SomeModel()
someInstance.getOthers({ someInstance.getOthers({
joinTableAttributes: { include: [ 'id' ] } joinTableAttributes: { include: [ 'id' ] }
}) })
/**
* Test for through options in creating a BelongsToMany association
*/
class Film extends Model {}
class Actor extends Model {}
Film.belongsToMany(Actor, {
through: {
model: 'FilmActors',
paranoid: true
}
})
Actor.belongsToMany(Film, {
through: {
model: 'FilmActors',
paranoid: true
}
})
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!