{"action":"created","issue":{"url":"https://api.github.com/repos/jhipster/generator-jhipster/issues/3723","repository_url":"https://api.github.com/repos/jhipster/generator-jhipster","labels_url":"https://api.github.com/repos/jhipster/generator-jhipster/issues/3723/labels{/name}","comments_url":"https://api.github.com/repos/jhipster/generator-jhipster/issues/3723/comments","events_url":"https://api.github.com/repos/jhipster/generator-jhipster/issues/3723/events","html_url":"https://github.com/jhipster/generator-jhipster/issues/3723","id":160582991,"number":3723,"title":"Error in addForeignKeyConstraint when using overridden table names","user":{"login":"HemanthD","id":16206433,"avatar_url":"https://avatars.githubusercontent.com/u/16206433?v=3","gravatar_id":"","url":"https://api.github.com/users/HemanthD","html_url":"https://github.com/HemanthD","followers_url":"https://api.github.com/users/HemanthD/followers","following_url":"https://api.github.com/users/HemanthD/following{/other_user}","gists_url":"https://api.github.com/users/HemanthD/gists{/gist_id}","starred_url":"https://api.github.com/users/HemanthD/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/HemanthD/subscriptions","organizations_url":"https://api.github.com/users/HemanthD/orgs","repos_url":"https://api.github.com/users/HemanthD/repos","events_url":"https://api.github.com/users/HemanthD/events{/privacy}","received_events_url":"https://api.github.com/users/HemanthD/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2016-06-16T05:53:09Z","updated_at":"2016-06-17T08:16:35Z","closed_at":null,"body":"##### **Overview of the issue**\r\n\r\nLiquibase generated addForeignKeyConstraint seems to use the default entity-name-derived table name rather than the specified/ overridden tablename (using --table-name).\r\n\r\nConsider 2 entities Criteria and Option with one-to-many relationship. Both entities are generated using --table-name option as pa_criteria and pa_option. The foreign key generated in pa_option has referencedTableName=\"criteria\" (default table name of Criteria entity) instead of referencedTableName=\"pa_criteria\" (specified table name).\r\n\r\nLiquibase fails while creating the fk.\r\n\r\nliquibase.exception.DatabaseException: Cannot add foreign key constraint [Failed SQL: ALTER TABLE atlas.pa_option ADD CONSTRAINT fk_option_criteria_id FOREIGN KEY (criteria_id) REFERENCES atlas.criteria (id)]\r\n\tat liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:301)\r\n\r\n##### **Motivation for or Use Case** \r\n\r\nTable names can be overridden using --table-name. However, there seem to be an issue when creating references to such entities - requiring manual intervention.\r\n\r\n##### **JHipster Version(s)** \r\n\r\n3.3.0\r\n\r\n##### **JHipster configuration, a `.yo-rc.json` file generated in the root folder** \r\n\r\n[.yo-rc.json.txt](https://github.com/jhipster/generator-jhipster/files/317725/default.yo-rc.json.txt)\r\n\r\n##### **Entity configuration(s) `entityName.json` files generated in the `.jhipster` directory** \r\n\r\n[Criteria.json.txt](https://github.com/jhipster/generator-jhipster/files/317728/Criteria.json.txt)\r\n[Option.json.txt](https://github.com/jhipster/generator-jhipster/files/317727/Option.json.txt)\r\n\r\n##### **Browsers and Operating System** \r\n\r\nChrome on Win 8.1 with MySQL 5.7\r\n\r\n##### **Reproduce the error** \r\n\r\n    save attached Criteria.json and Option.json in .jhipster/\r\n    yo jhipster:entity criteria --table-name pa_criteria --regenerate\r\n    yo jhipster:entity option --table-name pa_option --regenerate\r\n    check the xxxx_added_entity_constraints_Option.xml - it will have the incorrect referencedTableName=\"criteria\"\r\n    launch the app\r\n    Liquibase will throw an error while processing the xxxx_added_entity_constraints_Option.xml\r\n    \r\n##### **Related issues** \r\n\r\nDid not find any similar cases.\r\n\r\n##### **Suggest a Fix** \r\n\r\nThe issue could be with the below piece of code\r\n\r\n    <addForeignKeyConstraint baseColumnNames=\"<%= getColumnName(relationshipName) %>_id\"\r\n                             baseTableName=\"<%= entityTableName %>\"\r\n                             constraintName=\"<%= constraintName %>\"\r\n                             referencedColumnNames=\"id\"\r\n                             referencedTableName=\"<%\r\n                             var otherEntityTable = getTableName(otherEntityName);\r\n                             if (otherEntityTable == 'user') { %>jhi_user<% } else { %><%=otherEntityTable %><% } %>\"/>\r\n\r\nin generator-jhipster/generators/entity/templates/src/main/resources/config/liquibase/changelog/_added_entity_constraints.xml which is using _otherEntityName_ for _getTableName()_. Perhaps, it should use the *'entityTableName'* attribute from the _otherEntity.json_\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/jhipster/generator-jhipster/issues/comments/226709061","html_url":"https://github.com/jhipster/generator-jhipster/issues/3723#issuecomment-226709061","issue_url":"https://api.github.com/repos/jhipster/generator-jhipster/issues/3723","id":226709061,"user":{"login":"jdubois","id":316835,"avatar_url":"https://avatars.githubusercontent.com/u/316835?v=3","gravatar_id":"","url":"https://api.github.com/users/jdubois","html_url":"https://github.com/jdubois","followers_url":"https://api.github.com/users/jdubois/followers","following_url":"https://api.github.com/users/jdubois/following{/other_user}","gists_url":"https://api.github.com/users/jdubois/gists{/gist_id}","starred_url":"https://api.github.com/users/jdubois/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jdubois/subscriptions","organizations_url":"https://api.github.com/users/jdubois/orgs","repos_url":"https://api.github.com/users/jdubois/repos","events_url":"https://api.github.com/users/jdubois/events{/privacy}","received_events_url":"https://api.github.com/users/jdubois/received_events","type":"User","site_admin":false},"created_at":"2016-06-17T08:16:35Z","updated_at":"2016-06-17T08:16:35Z","body":"Yes, overriding table names is a source of issues... and I don't even understand what you gain from it."}}
{"actor":{"display_login":"jdubois"}}