Skip to content

Commit 003fa39

Browse files
committed
feat: reject relation generation when same relation name and foreign key
Signed-off-by: Muhammad Aaqil <aaqilcs102@gmail.com>
1 parent c3d08a6 commit 003fa39

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

packages/cli/generators/relation/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,17 @@ module.exports = class RelationGenerator extends ArtifactGenerator {
697697
),
698698
},
699699
]).then(props => {
700+
if (
701+
props.relationName === this.artifactInfo.foreignKeyName ||
702+
this.artifactInfo.relationName === this.artifactInfo.foreignKeyName
703+
) {
704+
/* istanbul ignore next */
705+
return this.exit(
706+
new Error(
707+
`relation name ${props.relationName} cannot be the same as foreign key name ${this.artifactInfo.foreignKeyName}.`,
708+
),
709+
);
710+
}
700711
debug(`props after relation name prompt: ${inspect(props)}`);
701712
// checks if the relation name already exists
702713
this.artifactInfo.srcRepositoryFile = path.resolve(

packages/cli/test/integration/generators/relation.belongs-to.integration.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,34 @@ describe('lb4 relation', /** @this {Mocha.Suite} */ function () {
5858
).to.be.rejectedWith(/No models found/);
5959
});
6060

61+
it('rejects relation when relation name is the same as foreign key name', async () => {
62+
await sandbox.reset();
63+
const prompt = {
64+
relationType: 'belongsTo',
65+
sourceModel: 'Order',
66+
destinationModel: 'Customer',
67+
foreignKeyName: 'customerId',
68+
relationName: 'customerId', // intentionally same as foreignKeyName
69+
};
70+
71+
return expect(
72+
testUtils
73+
.executeGenerator(generator)
74+
.inDir(sandbox.path, () =>
75+
testUtils.givenLBProject(sandbox.path, {
76+
additionalFiles: SANDBOX_FILES,
77+
}),
78+
)
79+
.withOptions(options)
80+
.withPrompts(prompt),
81+
// Now that the bug is fixed, both values interpolate and we can assert the full message
82+
).to.be.rejectedWith(
83+
new RegExp(
84+
`relation name ${prompt.relationName} cannot be the same as foreign key name ${prompt.foreignKeyName}`,
85+
),
86+
);
87+
});
88+
6189
context('generates model relation with default values', () => {
6290
const promptArray = [
6391
{

0 commit comments

Comments
 (0)