-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unify logic for creation of foreign keys (#27608)
and regenerated snapshots Fix #27598
- Loading branch information
Showing
5 changed files
with
113 additions
and
135 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
81 changes: 81 additions & 0 deletions
81
.../templates/src/main/resources/config/liquibase/changelog/add_relationship_constraints.ejs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<%# | ||
Copyright 2013-2024 the original author or authors from the JHipster project. | ||
This file is part of the JHipster project, see https://www.jhipster.tech/ | ||
for more information. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
https://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
-%> | ||
<%_ | ||
const relationshipType = relationship.relationshipType, | ||
relationshipName = relationship.relationshipName, | ||
ownerSide = relationship.ownerSide, | ||
otherEntityTableName = relationship.otherEntityTableName, | ||
onDelete = relationship.onDelete, | ||
onUpdate = relationship.onUpdate; | ||
if (relationshipType === 'many-to-one' || (relationshipType === 'one-to-one' && ownerSide)) { | ||
const constraintName = this.getFKConstraintName(entity.entityTableName, relationshipName, prodDatabaseType); | ||
let baseColumnNames; | ||
let referencedColumnNames; | ||
if (relationshipType === 'one-to-one' && ownerSide && relationship.id === true) { | ||
baseColumnNames = relationship.otherEntity.primaryKey.fields.map(field => field.columnName).join(','); | ||
referencedColumnNames = relationship.otherEntity.primaryKey.fields.map(field => field.columnName).join(','); | ||
} else if (relationship.otherEntity) { | ||
baseColumnNames = relationship.otherEntity.primaryKey.fields.map(field => relationship.columnName + '_' + field.columnName).join(','); | ||
referencedColumnNames = relationship.otherEntity.primaryKey.fields.map(field => field.columnName).join(','); | ||
} _%> | ||
<addForeignKeyConstraint baseColumnNames="<%= baseColumnNames %>" | ||
baseTableName="<%= entity.entityTableName %>" | ||
constraintName="<%= constraintName %>" | ||
referencedColumnNames="<%= referencedColumnNames %>" | ||
referencedTableName="<%= otherEntityTableName %>" | ||
<%_ if (onDelete) { _%> | ||
onDelete="<%= onDelete %>" | ||
<%_ } _%> | ||
<%_ if (onUpdate) { _%> | ||
onUpdate="<%= onUpdate %>" | ||
<%_ } _%> | ||
/> | ||
<%_ } else if (relationship.shouldWriteJoinTable) { _%> | ||
<addForeignKeyConstraint baseColumnNames="<%= entity.primaryKey.fields.map(field => entity.entityTableName + '_' + field.columnName).join(', ') %>" | ||
baseTableName="<%= relationship.joinTable.name %>" | ||
constraintName="<%= relationship.joinTable.constraintName %>" | ||
referencedColumnNames="<%= entity.primaryKey.fields.map(field => field.columnName).join(', ') %>" | ||
referencedTableName="<%= entity.entityTableName %>" | ||
<%_ if (onDelete) { _%> | ||
onDelete="<%= onDelete %>" | ||
<%_ } _%> | ||
<%_ if (onUpdate) { _%> | ||
onUpdate="<%= onUpdate %>" | ||
<%_ } _%> | ||
/> | ||
<addForeignKeyConstraint baseColumnNames="<%= relationship.otherEntity.primaryKey.fields.map(field => relationship.columnName + '_' + field.columnName).join(', ') %>" | ||
baseTableName="<%= relationship.joinTable.name %>" | ||
constraintName="<%= relationship.joinTable.otherConstraintName %>" | ||
referencedColumnNames="<%= relationship.otherEntity.primaryKey.fields.map(field => field.columnName).join(', ') %>" | ||
referencedTableName="<%= relationship.otherEntity.entityTableName %>" | ||
<%_ if (relationship.otherRelationship) { | ||
// User object is not supporting this right now | ||
_%> | ||
<%_ if (relationship.otherRelationship.onDelete) { _%> | ||
onDelete="<%= relationship.otherRelationship.onDelete %>" | ||
<%_ } _%> | ||
<%_ if (relationship.otherRelationship.onUpdate) { _%> | ||
onUpdate="<%= relationship.otherRelationship.onUpdate %>" | ||
<%_ } _%> | ||
<%_ } _%> | ||
/> | ||
<%_ } _%> |
This file contains 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
This file contains 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