laravel“SQLSTATE [HY000]:一般错误:1没有这样的表:user”但是'users'表存在

时间:2018-05-18 15:38:07

标签: php laravel sqlite

我在Windows 8.1上运行Laravel 5.2,使用带有php 7.2的XAMPP,我正在尝试使用laravel auth register form和sqlite数据库注册用户。但是,当我尝试向表users插入新记录时,我收到了错误。

  

SQLSTATE [HY000]:一般错误:1没有这样的表:user

当我迁移数据库时,它会创建用户表。但是当我尝试在users表中使用寄存器表单插入新记录时,它会尝试访问user表。所以我在数据库中创建了user表,它运行正常,但是记录被插入到users表而不是user表中。

移植

public function up(){
    Schema::create('users', function (Blueprint $table) {
        $table->increments('user_id');
        $table->string('name');
        $table->string('role');
        $table->string('username');
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
        });
    }

public function down(){
    Schema::drop('users');
}

用户模型

class User extends Authenticatable{
    protected $primaryKey = 'user_id';

    protected $fillable = [
        'name', 'role', 'username', 'password',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];
}

AuthController

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller{

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    protected $redirectTo = '/';
    protected $username = 'username';

    public function __construct()
    {
        $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
    }

    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'role' => 'required|max:7',
            'username' => 'required|unique:user',
            'password' => 'required|min:6|confirmed',
        ]);
    }

    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'role' => $data['role'],
            'username' =>$data['username'],
            'password' => bcrypt($data['password']),
        ]);
    }
}

感谢您的帮助,对不起英语感到抱歉。

4 个答案:

答案 0 :(得分:4)

您的问题在验证中:

class FragmentB : Fragment(){

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    buttonB.setOnClickListener {
        activity.supportFragmentManager.beginTransaction()
                .setCustomAnimations(R.animator.slide_up, R.animator.slide_down)
                .replace(R.id.container, FragmentB()).commit()
    }
}
}

您正在val df = sc .parallelize(Seq(("a", "1"), ("b", "2"))) .toDF("A", "B") df.printSchema root |-- A: string (nullable = true) |-- B: string (nullable = true) df.withColumn("B", 'B cast "bigint").printSchema 而不是df.withColumn("B", 'B cast "long").printSchema root |-- A: string (nullable = true) |-- B: long (nullable = true) 检查用户名是否唯一,请尝试以下操作:

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => 'required|max:255',
        'role' => 'required|max:7',
        'username' => 'required|unique:user',
        'password' => 'required|min:6|confirmed',
    ]);
}

此外,您可能希望在迁移中添加唯一约束。

答案 1 :(得分:0)

请参考: https://laravel.com/docs/5.6/eloquent#eloquent-model-conventions

如果您有型号用户,那么您必须在数据库中将该表作为“用户”而不是“用户”。

答案 2 :(得分:0)

我也有类似的错误。这是由于

Schema::create('tasks', function (Blueprint $table) {
    $table->foreignId('owner_id')->nullable()->constrained('user')->index();

代替(注意用户中的 s)

    $table->foreignId('owner_id')->nullable()->constrained('users')->index();

答案 3 :(得分:0)

在您的 users model 中添加 public $table = 'users' | public $table = 'user'