首页 > 解决方案 > 创建表时的Drupal错误

问题描述

我正在尝试在 .install 模块中创建一个新表,这就是我所做的:

$schema['commission_summary'] = array (
  'description' => 'Récapitulatif des commissions',
      'fields' => array(
           'commission_summary_id' => array(
              'description' => 'ID de commission',
              'type' => 'serial',
              'unsigned' => TRUE,
              'not null' => TRUE,
           ),
          'product_id' => array(
              'description' => 'ID du produit lié',
              'type' => 'int',
              'not null' => TRUE,
              'default' => 0,
          ),
          'date_depart' => array(
              'description' => 'Date du debut du voyage',
              'type' => 'int',
              'not null' => TRUE,
              'default' => 0,
          ),
          'prestataire' => array(
              'description' => 'ID du partenaire lié (Agence)',
              'type' => 'int',
              'not null' => TRUE,
              'default' => 0,
          ),
          'nb_participant_total' => array(
              'description' => 'Nombre de participants total',
              'type' => 'int',
              'not null' => TRUE,
              'default' => 0,
          ),
          'nb_participant_agence' => array(
              'description' => 'Nombre de participants de l\'agence ',
              'type' => 'int',
              'not null' => TRUE,
              'default' => 0,
          ),
      ),
      'primary key' => array('commission_summary_id'),
      'indexes' => array(
          'product_id' => array('product_id'),
          'partner_id' => array('prestataire')
      ),
  );
return $schema;

function ga_commission_update_7101() {
  $table = 'commission_summary';
  $schema = drupal_get_schema_unprocessed('commission_summary', $table);
  db_create_table('commission_summary', $schema);
}

问题是我得到一个错误,但我不知道为什么,我的语法看起来不错,不是吗?

我创建了其他类似的表,所以我不明白为什么这次它不起作用

这是我得到的错误:

Failed: PDOException : SQLSTATE[42000]: Syntax error or access violation: 
1064 You have an error in your SQL syntax; check the manual that 
corresponds to your MariaDB server version for the right syntax to use 
near ') ENGINE = InnoDB DEFAULT CHARACTER SET utf8' at line 1: CREATE 
TABLE {commission_summary} ) ENGINE = InnoDB DEFAULT CHARACTER SET utf8; 
Array ( ) dans db_create_table() (ligne 2776 dans 
/var/www/clients/client1/web84/web/includes/database/database.inc).

标签: databasepdodrupal

解决方案


您需要在 hook_update_n 中定义您的表,这将起作用:

 function ga_commission_update_7101() {
     $table = array(
        'description' => 'Récapitulatif des commissions',
        'fields' => array(
          'commission_summary_id' => array(
            'description' => 'ID de commission',
            'type' => 'serial',
            'unsigned' => TRUE,
            'not null' => TRUE,
          ),
          'product_id' => array(
            'description' => 'ID du produit lié',
            'type' => 'int',
            'not null' => TRUE,
            'default' => 0,
          ),
          'date_depart' => array(
            'description' => 'Date du debut du voyage',
            'type' => 'int',
            'not null' => TRUE,
            'default' => 0,
          ),
          'prestataire' => array(
            'description' => 'ID du partenaire lié (Agence)',
            'type' => 'int',
            'not null' => TRUE,
            'default' => 0,
          ),
          'nb_participant_total' => array(
            'description' => 'Nombre de participants total',
            'type' => 'int',
            'not null' => TRUE,
            'default' => 0,
          ),
          'nb_participant_agence' => array(
            'description' => 'Nombre de participants de l\'agence ',
            'type' => 'int',
            'not null' => TRUE,
            'default' => 0,
          ),
        ),
        'primary key' => array('commission_summary_id'),
        'indexes' => array(
          'product_id' => array('product_id'),
          'partner_id' => array('prestataire')
        ),
      );

      db_create_table('commission_summary', $table);
    }

推荐阅读