silverstripe / silverstripe-framework

Silverstripe Framework, the MVC framework that powers Silverstripe CMS
https://www.silverstripe.org
BSD 3-Clause "New" or "Revised" License
723 stars 821 forks source link

Polymorphic has_many relation query is too ambiguous #11194

Closed GuySartorelli closed 5 months ago

GuySartorelli commented 5 months ago

I got this error with installer 5.2.x-dev, linkfield 4.0.x-dev, and fluent ^7.

[Emergency] Uncaught SilverStripe\ORM\Connect\DatabaseException: Couldn't run query: SELECT "LinkField_Link"."ID", "LinkField_Link"."Sort" AS "_SortColumn0" FROM "LinkField_Link" LEFT JOIN "LinkField_Link_Localised" AS "LinkField_Link_Localised_en_US" ON "LinkField_Link"."ID" = "LinkField_Link_Localised_en_US"."RecordID" AND "LinkField_Link_Localised_en_US"."Locale" = ? WHERE ("OwnerClass" IN ('Page', 'SilverStripe\ErrorPage\ErrorPage', 'SilverStripe\FrameworkTest\Model\TestPage', 'SilverStripe\CMS\Model\RedirectorPage', 'SilverStripe\CMS\Model\VirtualPage', 'BasicElementalPage', 'SilverStripe\FrameworkTest\Model\MultiTabPage', 'SilverStripe\FrameworkTest\Model\SingleTabPage', 'SilverStripe\FrameworkTest\LinkField\PageTypes\LinkFieldTestPage', 'BasicFieldsTestPage', 'GridFieldTestPage', 'RelationFieldsTestPage', 'TestFileUploadPage')) AND ("OwnerRelation" = ?) AND ("LinkField_Link"."OwnerID" = ?) ORDER BY "_SortColumn0" ASC Column 'OwnerRelation' in where clause is ambiguous

Note that the OwnerID colun is mentioned as "LinkField_Link"."OwnerID", but the OwnerClass and OwnerRelation columns exclude the table name ("OwnerClass" and "OwnerRelation" respectively).

The OwnerClass column isn't reported as problematic in the error message, only OwnerRelation is - but they should both explicitly mention what table it is, since other scenarios might have joins which do make OwnerClass ambiguous as well.

Steps to reproduce

  1. install silverstripe/installer with 5.2.x-dev constraint
  2. Add silverstripe/linkfield with 4.0.x-dev constraint
  3. Add tractorcow/silverstripe-fluent with ^7 constraint
  4. Add a has_many relation on the Page class to Link like so:

    Click to expand code example ```php Link::class, ]; private static $has_many = [ 'HasManyLinks' => Link::class . '.Owner', ]; private static array $owns = [ 'HasOneLink', 'HasManyLinks', ]; private static array $cascade_deletes = [ 'HasOneLink', 'HasManyLinks', ]; private static array $cascade_duplicates = [ 'HasOneLink', 'HasManyLinks', ]; public function getCMSFields() { $fields = parent::getCMSFields(); // Don't forget to remove the auto-scaffolded fields! $fields->removeByName(['HasOneLinkID', 'HasManyLinks']); $fields->addFieldsToTab( 'Root.Main', [ LinkField::create('HasOneLink'), MultiLinkField::create('HasManyLinks'), // GridField::create('HasManyLinks')->setList($this->HasManyLinks()), ] ); return $fields; } } } ```
    • Note that this error happens with both MultiLinkField and GridField - so it's nothing specific to linkfield per se.
  5. Add the FluentVersionedExtension extension to Link:
    SilverStripe\LinkField\Models\Link:
      extensions:
        - TractorCow\Fluent\Extension\FluentVersionedExtension
  6. Add two locales to your site in the CMS (not sure if this is necessary but it is what I did)
  7. Try to navigate to any page edit form in the CMS - you should get the error.

PRs

GuySartorelli commented 5 months ago

@maxime-rainville I'm targetting 5.2 so that multi-relational has_many relations aren't completely busted with fluent on launch. Can you please approve that, given 5.2 is in a change freeze right now?

maxime-rainville commented 5 months ago

Presuming this is just a bug fix, I'm fine with it being merged post RC.