Errno:150“外键约束格式不正确”

时间:2017-11-17 作者:Mg10

我的外键引用有什么问题?是否未创建此表?

<?php
/*
** Plugin Name: ***
** Plugin URI: ***
** Description: ***
** Author: ***
** Version: ***
*/


class Database_plugin {

static function riverfisher_add_db_tables_activation()
{

      global $wpdb;
      global $wp_users;
      $table_registreringer = $wpdb->prefix . \'registreringer\';
      $charset_collate = utf8mb4_unicode_ci;

  if ( $wpdb->get_var( "SHOW TABLES LIKE \'{$table_registreringer}\'" ) != $table_registreringer ) {

$sql = "CREATE TABLE $table_registreringer 
    (
        reg_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
        dato date,
        billedeURL VARCHAR(80) NOT NULL,
        fiske_vaegt DECIMAL( 2,1 ) NOT NULL,
        fiske_laengde INT NOT NULL,         
        reg_user_id BIGINT UNSIGNED NOT NULL,
        reg_redskabs_id INT UNSIGNED NOT NULL,
        reg_koen_id INT UNSIGNED NOT NULL,
        reg_fisk_id INT UNSIGNED NOT NULL,
        reg_laks_id INT UNSIGNED NOT NULL,
        reg_cr_id INT UNSIGNED NOT NULL,
        PRIMARY KEY  (reg_id),
        FOREIGN KEY  (reg_user_id) REFERENCES wp_users(id)      
    ) 
    COLLATE $charset_collate";
echo $wpdb->last_error;  

    require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
    dbDelta( $sql );
  }; 

register_activation_hook( __FILE__, array( \'Database_plugin\' , \'riverfisher_add_db_tables_activation\' ) );
?>

1 个回复
最合适的回答,由SO网友:Drupalizeme 整理而成

它应该是大写的FOREIGN KEY (reg_user_id) REFERENCES wp_users(ID)

结束