We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 46556
    • 4 Posts
    I want to make a table with a compound primary key (a primary key consisting of two or more columns). In an older version of xPDO one could just add index="pk" to those fields. How is it done today? I could not find any documentation on that topic.

    Is the following correct?

    <object class="Class" table="table">
        <field key="primary1" dbtype="int" precision="8" phptype="integer" null="false" />
        <field key="primary1" dbtype="int" precision="8" phptype="integer" null="false" />
        <field key="data" dbtype="int" precision="8" phptype="integer" null="false" />
    
        <index alias="primary" name="primary" primary="true" type="BTREE">
            <column key="primary1" />
            <column key="primary2" />
        </index>
    </object>


    (By the way: What does type="BTREE" mean?)
      • 3749
      • 24,544 Posts
      There are some advantages to making your object xPDO-friendly by extending xPDOSimpleObject. That automatically gives you a primary autoincrement key of 'id'.

      Then your compound index can be a separate index, like this one from modResource:

      <index alias="content_ft_idx" name="content_ft_idx" primary="false" unique="false" type="FULLTEXT">
                  <column key="pagetitle" length="" collation="A" null="false" />
                  <column key="longtitle" length="" collation="A" null="false" />
                  <column key="description" length="" collation="A" null="false" />
                  <column key="introtext" length="" collation="A" null="true" />
                  <column key="content" length="" collation="A" null="true" />
              </index>


      If you do want to have a compound primary index, here's how it's done in the MODX schema:

      <index alias="PRIMARY" name="PRIMARY" primary="true" unique="true" type="BTREE">
                  <column key="ancestor" length="" collation="A" null="false" />
                  <column key="descendant" length="" collation="A" null="false" />
              </index>
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 46556
        • 4 Posts
        Thank you for your reply. I think that solves my problem.