错误:SQLSTATE [42S22]:找不到列:1054'字段列表'中的未知列'details.id'

时间:2013-07-12 06:58:53

标签: mysql cakephp elasticsearch

我使用cakephp-elasticsearch插件来索引和搜索mysql Db。我从这个链接开始遵循教程:

https://github.com/dkullmann/CakePHP-Elastic-Search-DataSource

我创建了“详情”模块:

<?php
class details extends AppModel {

   public $useDbConfig = 'index';
public $useType = 'details';

    public $_mapping = array(
        'name' => array('type' => 'string'),
        'addr' => array('type' => 'string'),
        'phno' => array('type' => 'string'),
        'city' => array('type' => 'string'),
        'state' => array('type' => 'string'),

    );

    public function elasticMapping() {
        return $this->_mapping;
    }
}
?>

要开始索引记录,我使用了以下命令:

Console/cake Elastic.elastic index details

我收到以下错误:

Retrieving data from mysql starting on 1970-01-01 00:00:00
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'details.id' in 'field list'
#0 /var/www/cakephp/lib/Cake/Model/Datasource/DboSource.php(460): PDOStatement->execute(Array)
#1 /var/www/cakephp/lib/Cake/Model/Datasource/DboSource.php(426): DboSource->_execute('SELECT `details...', Array)
#2 /var/www/cakephp/lib/Cake/Model/Datasource/DboSource.php(669): DboSource->execute('SELECT `details...', Array, Array)
#3 /var/www/cakephp/lib/Cake/Model/Datasource/DboSource.php(1080): DboSource->fetchAll('SELECT `details...', false)
#4 /var/www/cakephp/lib/Cake/Model/Model.php(2696): DboSource->read(Object(details), Array)
#5 /var/www/cakephp/app/Plugin/Elastic/Console/Command/ElasticShell.php(288): Model->find('all', Array)
#6 /var/www/cakephp/lib/Cake/Console/Shell.php(389): ElasticShell->index()
#7 /var/www/cakephp/lib/Cake/Console/ShellDispatcher.php(200): Shell->runCommand('index', Array)
#8 /var/www/cakephp/lib/Cake/Console/ShellDispatcher.php(68): ShellDispatcher->dispatch()
#9 /var/www/cakephp/app/Console/cake.php(37): ShellDispatcher::run(Array)
#10 {main}

我没有任何名为id的字段..我会做什么错?请帮帮我!!

1 个答案:

答案 0 :(得分:1)

所有数据库表都需要主键

默认情况下,该字段的名称为id。如果表具有不同的主键,则需要设置primaryKey属性,以便Cake知道它是什么:

class MyModel extends AppModel {

    $primaryKey = 'odd';

}

是常规的

  

我会做什么错误?

问题中的代码还有其他错误,即:

模型名称是单数

所以,class details - &gt; class detail

在查找要使用的表时,Cake会自动使用复数下划线形式(details)。

班级名称是CameBacked

所以class detail - &gt; class Detail

本书中有关于CakePHP's conventions的更多信息。

ES plugin的文档中也隐含地强调了这一点,比较了问题中的cli调用:

Console/cake Elastic.elastic index details
                                   ^ lower case plural model name

使用ES插件自述文件中的一个:

Console/cake Elastic.elastic index Contact
                                   ^ Correct case and singular

不遵循惯例是查找不一致行为的简便方法。

相关问题