SQLSTATE [42S22]:找不到列:1054 未知列 - Laravel

Posted

技术标签:

【中文标题】SQLSTATE [42S22]:找不到列:1054 未知列 - Laravel【英文标题】:SQLSTATE[42S22]: Column not found: 1054 Unknown column - Laravel 【发布时间】:2014-01-09 18:23:18 【问题描述】:

我正在使用框架 Laravel。

我有 2 个表(用户和成员)。当我想登录时,我收到错误消息:

SQLSTATE[42S22]:未找到列:1054 中的未知列 'user_email' 'where 子句' (SQL: select * from members where user_email = ? 限制 1) (绑定:数组 (0 => 'test@hotmail.com', ))

表用户

CREATE TABLE IF NOT EXISTS `festival_aid`.`users` (
  `user_id` BIGINT NOT NULL AUTO_INCREMENT,
  `user_email` VARCHAR(45) NOT NULL,
  `user_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `user_modified` TIMESTAMP NULL,
  `user_deleted` TIMESTAMP NULL,
  `user_lastlogin` TIMESTAMP NULL,
  `user_locked` TIMESTAMP NULL,
  PRIMARY KEY (`user_id`),
  UNIQUE INDEX `user_email_UNIQUE` (`user_email` ASC),
ENGINE = InnoDB;

表成员

CREATE TABLE IF NOT EXISTS `festival_aid`.`members` (
  `member_id` BIGINT NOT NULL AUTO_INCREMENT,
  `member_password` CHAR(32) NOT NULL,
  `member_salt` CHAR(22) NOT NULL,
  `member_token` VARCHAR(128) NULL,
  `member_confirmed` TIMESTAMP NULL,
  `user_id` BIGINT NOT NULL,
  PRIMARY KEY (`member_id`, `user_id`),
  INDEX `fk_members_users1_idx` (`user_id` ASC),
  CONSTRAINT `fk_members_users1`
    FOREIGN KEY (`user_id`)
    REFERENCES `festival_aid`.`users` (`user_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

迁移用户

public function up()
    
        Schema::table('users', function(Blueprint $table)
        
            $table->increments('user_id');

            $table->string('user_email');
            $table->timestamp('user_created');
            $table->timestamp('user_modified');
            $table->timestamp('user_deleted');
            $table->timestamp('user_lastlogin');
            $table->timestamp('user_locked');
        );
    

迁移成员

public function up()
    
        Schema::table('members', function(Blueprint $table)
        
            $table->increments('member_id');

            $table->string('member_password');
            $table->string('member_salt');
            $table->string('member_token');

            $table->foreign('user_id')
                ->references('id')->on('users');
            //->onDelete('cascade');

            $table->timestamp('member_confirmed');
        );
    

模型用户

class User extends Eloquent 

    protected $table = 'users';

    /**
     * The primary key for the model.
     *
     * @var string
     */
    protected $primaryKey = 'user_id';

    public $timestamps = false;

模型成员

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class Member extends Eloquent implements UserInterface, RemindableInterface 

    protected $table = 'members';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('member_password');

    /**
     * Get the unique identifier for the user.
     *
     * @return mixed
     */
    public function getAuthIdentifier()
    
        return $this->getKey();
    

    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    
        return $this->member_password;
    

    /**
     * Get the e-mail address where password reminders are sent.
     *
     * @return string
     */
    public function getReminderEmail()
    
        return $this->email;
    

    /**
     * The primary key for the model.
     *
     * @var string
     */
    protected $primaryKey = 'member_id';

    public $timestamps = false;

    public function users()
    
        return $this->hasOne('User');
    

成员模型使用: 使用 Illuminate\Auth\UserInterface;

<?php namespace Illuminate\Auth;

interface UserInterface 

    /**
     * Get the unique identifier for the user.
     *
     * @return mixed
     */
    public function getAuthIdentifier();

    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword();


控制器

public function store()
    
        $input = Input::all();

        $rules = array('user_email' => 'required', 'member_password' => 'required');

        $v = Validator::make($input, $rules);

        if($v->passes())
        
            $credentials = array('user_email' => $input['user_email'], 'member_password' => $input['member_password']);

            if(Auth::attempt($credentials))
            
                return Redirect::to('/home');

             else 

                return Redirect::to('login');
            

         else 

            return Redirect::to('login')->withErrors($v);
        
    

auth.php

return array(

    /*
    |--------------------------------------------------------------------------
    | Default Authentication Driver
    |--------------------------------------------------------------------------
    |
    | This option controls the authentication driver that will be utilized.
    | This drivers manages the retrieval and authentication of the users
    | attempting to get access to protected areas of your application.
    |
    | Supported: "database", "eloquent"
    |
    */

    'driver' => 'eloquent',

    /*
    |--------------------------------------------------------------------------
    | Authentication Model
    |--------------------------------------------------------------------------
    |
    | When using the "Eloquent" authentication driver, we need to know which
    | Eloquent model should be used to retrieve your users. Of course, it
    | is often just the "User" model but you may use whatever you like.
    |
    */

    'model' => 'Member',

    /*
    |--------------------------------------------------------------------------
    | Authentication Table
    |--------------------------------------------------------------------------
    |
    | When using the "Database" authentication driver, we need to know which
    | table should be used to retrieve your users. We have chosen a basic
    | default value but you may easily change it to any table you like.
    |
    */

    'table' => 'members',

    /*
    |--------------------------------------------------------------------------
    | Password Reminder Settings
    |--------------------------------------------------------------------------
    |
    | Here you may set the settings for password reminders, including a view
    | that should be used as your password reminder e-mail. You will also
    | be able to set the name of the table that holds the reset tokens.
    |
    | The "expire" time is the number of minutes that the reminder should be
    | considered valid. This security feature keeps tokens short-lived so
    | they have less time to be guessed. You may change this as needed.
    |
    */

    'reminder' => array(

        'email' => 'emails.auth.reminder',

        'table' => 'password_reminders',

        'expire' => 60,

    ),

);

我在这里做错了什么?

【问题讨论】:

与 Auth::attempt($credentials) 相关的代码在哪里? 这不是我的 Model: Member 中的代码吗? 我不熟悉框架,但是 Auth::attempt 是对 Auth 类的尝试方法的静态调用,而不是对 Member::something Member 模型使用这个:use Illuminate\Auth\UserInterface; 【参考方案1】:

你已经配置了auth.php 并使用了members 表进行身份验证,但是members 表中没有user_email 字段所以,Laravel 说

SQLSTATE[42S22]:未找到列:1054 中的未知列 'user_email' 'where 子句' (SQL: select * from members where user_email = ? limit 1) (绑定: 数组 (0 => 'test@hotmail.com', ))

因为它试图匹配members 表中的user_email,但它不存在。根据您的auth 配置,laravel 正在使用members 表进行身份验证,而不是users 表。

【讨论】:

这就是重点,你必须寻找你的验证(exists[table]),你必须确保你验证的表的行匹配。 什么意思 :-> SQLSTATE[42S22]: Column not found: 1054 Unknown column '''' in where 子句, where class is :-> WHERE (' ' = ''' '),我可以得到帮助@The Alpha 在该表中,提到的列不可用。 @宝石 实际上我从soap API得到这个错误,我如何在soap中更新。 有人已经在 Magento 论坛发帖,community.magento.com/t5/Magento-1-x-Programming/…【参考方案2】:

您的成员表中没有名为 user_email 的字段 ... 至于为什么,我不确定代码“看起来”是否应该尝试加入不同的字段

Auth::attempt 方法是否执行模式连接? 运行grep -Rl 'class Auth' /path/to/framework 并找到attempt 方法在哪里以及它的作用。

【讨论】:

user_email 字段位于 users 表中。 Auth::attempt 只会将您重定向到主页或返回登录页面。 不,这是由于对 Auth::attempt 的成功调用(读取返回 true)而指定的操作。你需要找到包含'class Auth'的代码 class Auth 除了作为 Laravel 的 IoC 容器的外观之外不存在,该容器具有与授权方法的绑定。 Auth::attempt 使用 laravel 配置/扩展(通过闭包)来构建 Auth 实现。但是在成员表中没有user_email 字段是正确的。 :)【参考方案3】:

尝试更改成员类的位置

public function users() 
    return $this->hasOne('User');
 

return $this->belongsTo('User');

【讨论】:

这是我的修复,虽然我有同样的错误,但在 belongsTo 和 hasMany 之间混淆了,谢谢。

以上是关于SQLSTATE [42S22]:找不到列:1054 未知列 - Laravel的主要内容,如果未能解决你的问题,请参考以下文章

SQLSTATE [42S22]:找不到列:1054 未知列 '0' where 子句

SQLSTATE [42S22]:找不到列:1054 未知列 laravel 5.1

SQLSTATE [42S22]:找不到列:1054 'where 子句'中的未知列'user_id'

Laravel 删除数据 - SQLSTATE [42S22]:找不到列:1054 未知列

SQLSTATE [42S22]:找不到列:1054 未知列 'role_not' 错误

SQLSTATE [42S22]:找不到列:在 laravel livewire 项目中