Problem/Motivation

I get the following fatal error message when trying to check links on entities that have no bundle.
Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'base.' in 'where clause'.

Entities without a bundle are considered as having exactly one bundle. The bundle is the same the entity type id but they don't have a bundle key so the following lines result in the exception.

      if (!empty($bundle)) {
        $query->condition('base.' . $entityType->getKey('bundle'), $bundle);
      }

Steps to reproduce

  1. Install the module
  2. Add a link field on the User entity type (or another entity type that has no bundles.)
  3. Enable "Scan for broken links" on the new field
  4. Visit the configuration page of the Linkchecker module

Proposed resolution

Also check that the bundle key is not empty.

      if (!empty($bundle) && $entityType->getKey('bundle')) {
        $query->condition('base.' . $entityType->getKey('bundle'), $bundle);
      }
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Dozz created an issue. See original summary.

dozz’s picture

Status: Active » Needs review
enchufe’s picture

Rerolled patch #1 for branch 2.1.x.

eiriksm’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Thanks for your work here people ♥️🙏

Is there a core or contrib entity type to use to reproduce this?

This way it should be easy to add a test, which I think we need here

shivam_tiwari’s picture

Version: 2.0.x-dev » 2.1.x-dev
Assigned: Unassigned » shivam_tiwari

I am working here.

shivam_tiwari’s picture

Assigned: shivam_tiwari » Unassigned
Status: Needs work » Needs review
StatusFileSize
new33.47 KB
new189.52 KB

I got issue as mentioned here. Created MR 107. It is working for me. Adding screenshots of before and after applying changes. Moving this to Needs review state as pipeline is green.

eiriksm’s picture

Status: Needs review » Needs work

There are still no clear steps to reproduce this issue, and still no tests 🤓✌️

eiriksm’s picture

Oh there are steps here now, awesome!

That should make it trivial to write a test then!

jwilson3 made their first commit to this issue’s fork.

jwilson3’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests

I also hit this issue and confirmed the MR as-is (without tests) does in fact fix the bug.

I took a stab at adding a test using the core User entity, since it is the most obvious choice of a bundleless entity type and matches the reproduction steps in the issue summary.

The test itself is tightly scoped to just a Link field to the User entity, create a user, and assert that LinkExtractorBatch::getTotalEntitiesToProcess() no longer throws the SQL error and correctly returns 1.

I ran tests locally and confirmed without the codefix the fatal error is triggered. I then ran the test again with the code fix and the test passes.

$ ddev phpunit tests/src/Kernel/LinkcheckerBundlelessEntityBatchTest.php
PHPUnit 11.5.55 by Sebastian Bergmann and contributors.
Runtime:       PHP 8.3.27
.                                                                   1 / 1 (100%)
Time: 00:03.781, Memory: 6.00 MB
OK, but there were issues!
Tests: 1, Assertions: 5, PHPUnit Deprecations: 2.

without fix:

ddev phpunit tests/src/Kernel/LinkcheckerBundlelessEntityBatchTest.php
PHPUnit 11.5.55 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.3.27

E                                                                   1 / 1 (100%)

Time: 00:06.754, Memory: 6.00 MB

There was 1 error:

1) Drupal\Tests\linkchecker\Kernel\LinkcheckerBundlelessEntityBatchTest::testGetTotalEntitiesToProcessForUserLinkField
Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'base.' in 'WHERE': SELECT COUNT(*) AS "expression"
FROM
(SELECT 1 AS "expression"
FROM
"test45522144users" "base"
WHERE "base"."" = :db_condition_placeholder_0) "subquery"; Array
(
    [:db_condition_placeholder_0] => user
)


/var/www/html/web/core/modules/mysql/src/Driver/Database/mysql/ExceptionHandler.php:96
/var/www/html/web/core/modules/mysql/src/Driver/Database/mysql/ExceptionHandler.php:26
/var/www/html/web/core/lib/Drupal/Core/Database/Connection.php:669
/var/www/html/web/core/lib/Drupal/Core/Database/Query/Select.php:521
/var/www/html/src/LinkExtractorBatch.php:173
/var/www/html/tests/src/Kernel/LinkcheckerBundlelessEntityBatchTest.php:82

Caused by
PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'base.' in 'WHERE'

/var/www/html/web/core/lib/Drupal/Core/Database/Statement/PdoTrait.php:109
/var/www/html/web/core/lib/Drupal/Core/Database/StatementWrapperIterator.php:89

However, both locally, and now on the CI pipeline, I see 10+ unrelated test failures when running all tests (as opposed to just the single new test I initially ran).

This MR branch is somewhat behind the latest 2.1.x branch, so I decided to see what would happen merging latest 2.1.x into my working copy of the MR branch and sadly, I still see the same failures. These failures are fundamentally unrelated to this issue, and they would affect ANY MR in the issue queue, and therefore this needs a follow-up issue.

I can safely remove the "Needs tests" tag now, and putting into NR, in case a maintainer might want to merge in spite of the unrelated test failures.

jwilson3’s picture

I fixed the unrelated CI failures in a separate issue, that I hope can get reviewed and merged so we can see tests working here too. Thanks!

#3582328: Fix broken tests blocking CI pipeline success