<%#
 Copyright 2013-2025 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,
bcConfig = relationship.bcConfig || {}, // add by plus
onDelete = relationship.onDelete,
onUpdate = relationship.onUpdate;
// add by begcode start
if (!(((applicationTypeMonolith || applicationTypeMicroservice) && multiModule && entity.moduleName && entity.moduleName !== relationship.otherEntity.moduleName) ||
                (applicationTypeMicroservice && !(relationship.otherEntity.applications.includes('*') || relationship.otherEntity.applications.includes(baseName)))
        )) {
// add by begcode end
if (relationshipType === 'many-to-one' && !bcConfig.unidirectionSkip || (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 %>"
            <%_ } _%>
        <%_ } _%>
         />
<%_ } _%>
<%_ } _%><%# add by begcode _%>
