Skip to content

Conversation

@soleinjast
Copy link
Contributor

Problem

The DatabaseQuery MCP tool doesn't respect Laravel's table prefix configuration when executing raw SQL queries. When apps use a prefix like 'prefix' => 'arpg_', queries fail with "table not found" errors.

Solution

This PR adds automatic prefix application:

  • Retrieves prefix using $connection->getTablePrefix()
  • Adds addPrefixToQuery() method with regex to match table names after FROM, JOIN, INTO, UPDATE, TABLE keywords

Example:

-- Input (with prefix 'arpg_')
SELECT * FROM poe1_leagues
-- Output
SELECT * FROM arpg_poe1_leagues

Benefits

  • Fixes queries for apps using table prefixes
  • Backward compatible (no impact when prefix is empty)
  • No breaking changes

Testing

  • Updated tests to mock getTablePrefix()
  • All existing tests pass

@sakshamgorey
Copy link
Contributor

sakshamgorey commented Feb 7, 2026

Hey @soleinjast , thanks for the PR ,this is a solution for #316 and I ran into a blocker with this exact approach regarding CTE queries.

The issue is that the logic blindly prefixes table names, so it incorrectly prefixes CTE aliases too.

Example:

WITH recent_users AS (
    SELECT * FROM users
)
SELECT * FROM recent_users

If a prefix is active (e.g., app_), this results in SELECT * FROM app_recent_users, which throws a "Table not found" error. We might need a more robust way to exclude WITH aliases.

@soleinjast
Copy link
Contributor Author

Hey @soleinjast , thanks for the PR ,this is a solution for #316 and I ran into a blocker with this exact approach regarding CTE queries.

The issue is that the logic blindly prefixes table names, so it incorrectly prefixes CTE aliases too.

Example:

WITH recent_users AS (
    SELECT * FROM users
)
SELECT * FROM recent_users

If a prefix is active (e.g., app_), this results in SELECT * FROM app_recent_users, which throws a "Table not found" error. We might need a more robust way to exclude WITH aliases.

Hey @sakshamgorey!
Exactly - the issue was that the logic didn't differentiate between real tables and CTE aliases. The updated implementation now identifies CTE names first and skips them during the prefixing process, which resolves the "Table not found" errors you encountered.

@pushpak1300 pushpak1300 merged commit 70e5b33 into laravel:main Feb 9, 2026
15 checks passed
@pushpak1300
Copy link
Member

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants