Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/cli/generators/relation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,17 @@ module.exports = class RelationGenerator extends ArtifactGenerator {
),
},
]).then(props => {
if (
props.relationName === this.artifactInfo.foreignKeyName ||
this.artifactInfo.relationName === this.artifactInfo.foreignKeyName
) {
/* istanbul ignore next */
return this.exit(
new Error(
`relation name ${props.relationName} cannot be the same as foreign key name ${this.artifactInfo.foreignKeyName}.`,
),
);
}
debug(`props after relation name prompt: ${inspect(props)}`);
// checks if the relation name already exists
this.artifactInfo.srcRepositoryFile = path.resolve(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,34 @@ describe('lb4 relation', /** @this {Mocha.Suite} */ function () {
).to.be.rejectedWith(/No models found/);
});

it('rejects relation when relation name is the same as foreign key name', async () => {
await sandbox.reset();
const prompt = {
relationType: 'belongsTo',
sourceModel: 'Order',
destinationModel: 'Customer',
foreignKeyName: 'customerId',
relationName: 'customerId', // intentionally same as foreignKeyName
};

return expect(
testUtils
.executeGenerator(generator)
.inDir(sandbox.path, () =>
testUtils.givenLBProject(sandbox.path, {
additionalFiles: SANDBOX_FILES,
}),
)
.withOptions(options)
.withPrompts(prompt),
// Now that the bug is fixed, both values interpolate and we can assert the full message
).to.be.rejectedWith(
new RegExp(
`relation name ${prompt.relationName} cannot be the same as foreign key name ${prompt.foreignKeyName}`,
),
);
});

context('generates model relation with default values', () => {
const promptArray = [
{
Expand Down