Skip to content

fix: [DBForge] addForeignKey() Table prefix issue while using with database #9085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from

Conversation

Kyoiory
Copy link

@Kyoiory Kyoiory commented Jul 27, 2024

While adding a foreign key to any external table, it was getting miss formatted if we defined the DB prefix like db2.table_name is being formatted like prefix_db2.tablename and the query was ended up in malformed constraint error.

This fix will add prefix to the table name only.

Description
I have changed the logic of adding prefix for foreign tables and now prefix will only prepend with table name only (last index)

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

While adding a foreign key to any external table, it was getting miss formatted if we defined the DB prefix like
db2.table_name is being formatted like prefix_db2.tablename and the query was ended up in malformed constraint error.

This fix will add prefix to the table name only.
@Kyoiory Kyoiory changed the title Update Forge.php Fix Forge.php Jul 27, 2024
@kenjis
Copy link
Member

kenjis commented Jul 28, 2024

Thank you for sending this PR!

We expect the following in all Pull Requests (PRs).

Important

We expect all code changes or bug-fixes to be accompanied by one or more tests added to our test suite to prove the code works.

If pull requests do not comply with the above, they will likely be closed. Since we are a team of volunteers, we don't have any more time to work on the framework than you do. Please make it as painless for your contributions to be included as possible.

See https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/pull_request.md

@kenjis kenjis added the tests needed Pull requests that need tests label Jul 28, 2024
@kenjis
Copy link
Member

kenjis commented Jul 28, 2024

Why do you need to specify the table name like db2.table_name?
I don't think now CI4 supports such a name. There is no such usage in the user guide:
https://codeigniter4.github.io/CodeIgniter4/dbmgmt/forge.html#adding-foreign-keys

So I think it is just a misuse, not a bug.

@kenjis kenjis added the database Issues or pull requests that affect the database layer label Jul 28, 2024
@Kyoiory
Copy link
Author

Kyoiory commented Jul 28, 2024

People often use external table foreign relationships. Just like in my case where i wanted to add foreign key with tablea from databasea to tableb of databaseb, and was getting it formatted like "prefix_databaseb.tableb"

@kenjis
Copy link
Member

kenjis commented Jul 28, 2024

Okay, that makes sense.
But I'm not sure this is a bug fix or enhancement.

In any case, please fix all errors in GitHub Actions.

@kenjis
Copy link
Member

kenjis commented Jul 30, 2024

We have a command to fix coding style.

composer cs-fix

See https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/pull_request.md#php-style

@Kyoiory
Copy link
Author

Kyoiory commented Jul 30, 2024

I am not working with composer until SDLC. Right now i am at pre planning phase and only able to structure my code.

@kenjis
Copy link
Member

kenjis commented Jul 30, 2024

Sorry, I don't get what you mean by "until SDLC".

If you don't use Composer, you cannot run PHPUnit.
How could you write test code for this PR?

In any case, we do not accept PRs without test code.
Thank you.

@Kyoiory
Copy link
Author

Kyoiory commented Jul 31, 2024

Actually i ran the fixer couple of times (on standalone installation) and only change the specific 3 lines.

Kyoiory added 3 commits August 1, 2024 03:32
While inserting and updating the data table prefix was getting doubled if use with database name
like if we define the table name as "database.tablename" it working fine for selects but for updates and inserts it converting the table name such as "`database`.`pfix_``pfix_tablename".  So this minor fix will not append the prefix to the table name if it is already existed in table.
Ran cs-fix as per PSR-12 standards and updating the snippet only
@Kyoiory Kyoiory changed the title Fix Forge.php fix / feat: Forge.php Jul 31, 2024
@Kyoiory Kyoiory changed the title fix / feat: Forge.php fix: Table prefix issue while using with databas Jul 31, 2024
@datamweb
Copy link
Contributor

datamweb commented Aug 1, 2024

Please make sure to update the packages first with the following command.

composer update

Then run command :

git switch patch-1

Then run

composer cs-fix
git add system/Database/Forge.php system/Database/BaseConnection.php
git commit -m "style: fix code style"
git push origin patch-1

@kenjis kenjis changed the title fix: Table prefix issue while using with databas fix: [DBForge] addForeignKey() Table prefix issue while using with database Aug 1, 2024
@kenjis
Copy link
Member

kenjis commented Aug 1, 2024

By the way, can you write test code?
See https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/pull_request.md#unit-testing

We do not accept PRs without test code.

@kenjis
Copy link
Member

kenjis commented Aug 21, 2024

Important

We expect all code changes or bug-fixes to be accompanied by one or more tests
added to our test suite to prove the code works.

This PR does not have the necessary test code. If you want this PR to be reviewed, please add the test code.
See https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/pull_request.md#unit-testing

If the test is not added, this PR will be closed.

@kenjis kenjis closed this Aug 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
database Issues or pull requests that affect the database layer tests needed Pull requests that need tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants