【问题标题】:Drupal 7 hook_schema not installing database tableDrupal 7 hook_schema 没有安装数据库表
【发布时间】:2012-11-30 00:02:40
【问题描述】:

任何帮助都会很棒。

function request_gold_pack_schema() {
    $schema['request_gold_pack_customer_details'] = array(
        'description' => 'Table to store all customer details.',
        'fields' => array(     
            'rid' => array(
                'type' => 'int',  
                'not null' => TRUE, 
                'default' => 0,
                'auto increment' => TRUE
            ),
            'title' => array(
                'type' => 'varchar', 
                'length' => 10,
                'not null' => TRUE,
                'default' => ''
            ),
            'first_name' => array(
                'type' => 'varchar',
                'length' => 50, 
                'not null' => TRUE,
                'default' => ''
            ),
            'last_name' => array(
                'type' => 'varchar',
                'length' => 50,
                'not null' => TRUE,
                'default' => ''
            ),
            'house_name_no' => array(
                'type' => 'varchar', 
                'length' => 50,
                'not null' => TRUE,
                'default' => ''
            ),
            'street' => array(
                'type' => 'varchar',
                'length' => 160,
                'not null' => TRUE,
                'default' => ''
            ),
            'town' => array(
                'type' => 'varchar',
                'length' => 50,
                'not null' => TRUE, 
                'default' => ''
            ),
            'county' => array(
                'type' => 'varchar', 
                'length' => 50,
                'not null' => TRUE,
                'default' => ''
            ),
            'telephone' => array(
                'type' => 'int',
                'length' => 12,
                'not null' => TRUE,
                'default' => ''
            ),
            'email' => array(
                'type' => 'varchar', 
                'length' => 255,
                'not null' => TRUE,
                'default' => ''
            ),
            'date_registered' => array(
                'mysql_type' => 'DATETIME',
                'not null' => TRUE
            ),
            'primary' => array(
                'rid'
            )
        )
    );

    return $schema;
}

这给了我以下错误

注意:未定义的索引:输入 DatabaseSchema_mysql->processField()(/Users/richardskinner/Sites/www.goldrushmoney.com-local/httpdocs/includes/database/mysql/schema.inc 的第 205 行)。 注意:未定义索引:DatabaseSchema_mysql->processField() 中的 :normal(/Users/richardskinner/Sites/www.goldrushmoney.com-local/httpdocs/includes/database/mysql/schema.inc 的第 205 行)。 PDOException: SQLSTATE[42000]: 语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册以获取正确的语法,以便在 'DEFAULT NULL ) ENGINE = InnoDB DEFAULT CHARACTER SET utf8 COMMENT 'Table to stor' 在第 13 行:CREATE TABLE {request_gold_pack_customer_details} (rid INT NOT NULL DEFAULT 0, title VARCHAR(10) NOT NULL DEFAULT '', first_name VARCHAR(50) NOT NULL DEFAULT '', last_name VARCHAR(50) NOT NULL DEFAULT '', house_name_no VARCHAR(50) NOT NULL DEFAULT '', street VARCHAR(160) NOT NULL DEFAULT '', town VARCHAR(50) NOT NULL DEFAULT '', county VARCHAR(50) NOT NULL DEFAULT '', telephone INT NOT NULL DEFAULT '', email VARCHAR(255) NOT NULL DEFAULT '', date_registered DATETIME NOT NULL, primary DEFAULT NULL ) ENGINE = InnoDB 默认字符集 utf8 COMMENT '用于存储所有客户详细信息的表。'; db_create_table() 中的数组 ( )(/Users/richardskinner/Sites/www.goldrushmoney.com-local/httpdocs/includes/database/database.inc 的第 2688 行)。

几个小时以来一直在努力寻找解决方案。

谢谢。

【问题讨论】:

    标签: drupal-7 drupal-hooks drupal-schema


    【解决方案1】:

    这是您的主键的问题。您已将其列在字段数组中(不应该如此),并且应将其引用为“主键”而不是“主键”,如下所示:

    function request_gold_pack_schema(){
        $schema['request_gold_pack_customer_details'] = array(
            'description' => 'Table to store all customer details.',
            'fields' => array(     
                  // Your field definitions
             ),
             'primary key' => array(
                 'rid'
             )
        );
        return $schema;
    }
    

    查看 drupal.org 上的 schema api documentation

    我还建议将rid 字段设置为输入serial 并保留自动增量参数(Drupal 将处理该参数)。所以“摆脱”字段定义是这样的:

    'rid' => array(
        'type' => 'serial',  
        'unsigned' => TRUE, 
        'not null' => TRUE, 
    )
    

    【讨论】:

    • 实际上,在您给出的链接中的 {node} 示例中,主键是“字段”下列出的第一个键。这与我目前正在处理的示例模块中的 entity_example 相同。也许这只是主/主键问题?我可能错了——我还没有开始工作。
    【解决方案2】:

    您必须在 drupal 7 中为类型“int”指定大小...即

    'rid' => array(
    'type' => 'int',  
    'not null' => TRUE, 
    'size' => 'normal',
    'auto increment' => TRUE,
    ),
    

    尺寸可以是正常,小,大 检查 drupal 7 数据类型。

    【讨论】:

      猜你喜欢
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多