浏览文章

文章信息

Magento2 | 如何在 db_schema.xml 中添加外键 | How to add foreign key in db_schema.xml 10567

示例:

<?xml version="1.0" ?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="aiweline_socialinfluencermarketing_socialinfluencer" resource="default" engine="innodb"
           comment="aiweline_socialinfluencermarketing_socialinfluencer Table">
        <column xsi:type="smallint" name="socialinfluencer_id" padding="6" unsigned="true" nullable="false"
                identity="true" comment="Entity Id"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="socialinfluencer_id"/>
        </constraint>
        <column name="firstname" nullable="true" xsi:type="varchar" comment="姓" length="60"/>
        <column name="middlename" nullable="true" xsi:type="varchar" comment="中间名" length="60"/>
        <column name="lastname" nullable="true" xsi:type="varchar" comment="名" length="60"/>
        <column name="email" nullable="true" xsi:type="varchar" comment="网红联系邮箱" length="120"/>
        <column name="customer_id" nullable="true" xsi:type="int" comment="网红在本站的客户ID" identity="false"
                unsigned="true"/>
        <index referenceId="AIWELINE_SOCIALINFLUENCERMARKETING_SOCIALINFLUENCER_CUSTOMER_ID" indexType="btree">
            <column name="customer_id"/>
        </index>
        <column name="created_at" nullable="false" xsi:type="datetime" comment="添加时间" default="CURRENT_TIMESTAMP"/>
        <index referenceId="AIWELINE_SOCIALINFLUENCERMARKETING_SOCIALINFLUENCER_CREATED_AT" indexType="btree">
            <column name="created_at"/>
        </index>
        <column name="updated_at" nullable="true" xsi:type="datetime" comment="更新时间" default="CURRENT_TIMESTAMP"
                on_update="true"/>
        <column name="avatar" nullable="true" xsi:type="varchar" comment="网红头像" length="1024"/>
        <column name="info" nullable="true" xsi:type="varchar" comment="网红简介" length="1024"/>
        <column name="is_top" nullable="true" xsi:type="smallint" comment="是否置顶" default="0" unsigned="true"/>
        <column name="is_active" nullable="true" xsi:type="smallint" comment="状态" default="1" unsigned="true"/>
        <column name="stores" nullable="true" xsi:type="varchar" comment="店铺" length="255"/>
        <column name="type" nullable="true" xsi:type="varchar" comment="网红类型" default="star" length="255"/>
        <column name="sku" nullable="true" xsi:type="varchar" comment="独家产品SKU" default="" length="255"/>
        <column name="commission_rate" nullable="false" xsi:type="float" comment="佣金比率(百分比)" default="30"/>
        <column name="total_soon_earn" nullable="true" xsi:type="decimal" comment="即将赚取(订单尚未完成)" scale="4"
                precision="20" unsigned="false"/>
        <column name="total_earned" nullable="true" xsi:type="decimal" comment="总赚取" scale="4" precision="20"
                unsigned="false"/>
        <column name="total_extracted" nullable="true" xsi:type="decimal" comment="已提取" scale="4" precision="20"
                unsigned="false"/>
        <column name="total_extractable" nullable="true" xsi:type="decimal" comment="可提取" scale="4" precision="20"
                unsigned="false"/>
        <column name="total_orders" nullable="true" xsi:type="int" comment="订单数" unsigned="false"/>
    </table>
    <table name="aiweline_socialinfluencermarketing_influencerorder" resource="default" engine="innodb"
           comment="aiweline_socialinfluencermarketing_influencerorder Table">
        <column xsi:type="smallint" name="id" padding="6" unsigned="true" nullable="false" identity="true"
                comment="Entity Id"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="id"/>
        </constraint>
        <column name="influencerorder_id" nullable="true" xsi:type="int" comment="网红ID" identity="false"
                unsigned="true"/>
        <column name="order_id" nullable="true" xsi:type="int" comment="实际订单ID" identity="false" unsigned="true"/>
        <column name="created_at" nullable="true" xsi:type="datetime" comment="创建时间" default="CURRENT_TIMESTAMP"/>
        <index referenceId="AIWELINE_SOCIALINFLUENCERMARKETING_INFLUENCERORDER_CREATED_AT" indexType="btree">
            <column name="created_at"/>
        </index>
        <column name="updated_at" nullable="true" xsi:type="datetime" comment="更新时间" default="CURRENT_TIMESTAMP"
                on_update="true"/>
        <index referenceId="AIWELINE_SOCIALINFLUENCERMARKETING_INFLUENCERORDER_UPDATED_AT" indexType="btree">
            <column name="updated_at"/>
        </index>
        <column name="order_no" nullable="true" xsi:type="varchar" comment="订单编号" length="255"/>
        <constraint xsi:type="foreign"
                    referenceId="AIWELINE_SOCIALINFLUENCERMARKETING_INFLUENCERORDER_SOCIALINFLUENCER_ID"
                    table="aiweline_socialinfluencermarketing_influencerorder"
                    column="socialinfluencer_id" referenceTable="aiweline_socialinfluencermarketing_influencerorder"
                    referenceColumn="socialinfluencer_id" onDelete="CASCADE"/>
    </table>
    
</schema>

关键代码:

<constraint xsi:type="foreign"
                    referenceId="AIWELINE_SOCIALINFLUENCERMARKETING_INFLUENCERORDER_SOCIALINFLUENCER_ID"
                    table="aiweline_socialinfluencermarketing_influencerorder"
                    column="socialinfluencer_id" referenceTable="aiweline_socialinfluencermarketing_socialinfluencer"
                    referenceColumn="socialinfluencer_id" onDelete="CASCADE"/>


注意:table属性是当前要设置外键的表名。

原创