Custom Table Primary and Foreign Key

Support MB Custom Table Custom Table Primary and Foreign KeyResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #21146
    EricHedvatEricHedvat
    Participant

    I have created a custom table for meta fields using the guide.
    The ID field has been automatically added. It is a primary key, yet it displays the post id which the meta field belongs to.
    When I delete that post from wordpress, the custom table entries remain.
    How can i reconfigure it so 'ID' is an autoincrementing primary key, and 'post_id' is a foreign key, referencing the post id, and enabling the entry to be deleted when the post is deleted?

    Here is the code used for custom table

    add_action( 'init', 'prefix_create_table' );
    function prefix_create_table() {
        if ( ! class_exists( 'MB_Custom_Table_API' ) ) {
            return;
        }
        MB_Custom_Table_API::create( 'my_custom_table', array(
            'address' => 'TEXT NOT NULL',
            'phone'   => 'TEXT NOT NULL',
            'email'   => 'VARCHAR(20) NOT NULL',
        ) );
    }

    Thanks

    #21152
    Long NguyenLong Nguyen
    Moderator

    Hi,

    When you delete the post from Trash, it removes the row in the custom table as well. See my screen record https://www.loom.com/share/218bb42c044045468f7355e20a3aa6f6.

    You can also set KEY for another column by using the third parameter.

    MB_Custom_Table_API::create( 'my_custom_table', array(
        'address' => 'TEXT NOT NULL',
        'phone'   => 'TEXT NOT NULL',
        'email'   => 'VARCHAR(20) NOT NULL',
    ), array( 'email' ) );
    
    #21153
    EricHedvatEricHedvat
    Participant

    Working thanks!

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.