php - 找不到 Laravel 4 迁移基表

标签 php mysql laravel laravel-4

我正在尝试制作以下教程:https://medium.com/on-coding/e8d93c9ce0e2

当谈到运行时:

php artisan migrate

我收到以下错误:

[Exception]                                                                        
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel.user' doesn't   
exist (SQL: alter table `user` add `id` int unsigned not null auto_increment prim  
ary key, add `username` varchar(255) null, add `password` varchar(255) null, add   
`email` varchar(255) null, add `created_at` datetime null, add `updated_at` datet  
ime null) (Bindings: array (                                                       
))

数据库连接正常,迁移表创建成功。如您在错误消息中所见,数据库名称已更改。

对我来说很奇怪的是,它试图改变表——它不存在——而不是创建它。

这是我的其他文件,例如 UserModel、Seeder、Miggation 和 DB Config。

创建用户表:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUserTable extends Migration {

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::table('user', function(Blueprint $table)
    {

        $table->increments("id");

        $table
            ->string("username")
            ->nullable()
            ->default(null);
        $table
            ->string("password")
            ->nullable()
            ->default(null);
        $table
            ->string("email")
            ->nullable()
            ->default(null);
        $table
            ->dateTime("created_at")
            ->nullable()
            ->default(null);
        $table
            ->dateTime("updated_at")
            ->nullable()
            ->default(null);

    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('user', function(Blueprint $table)
    {
        Schema::dropIfExists("user");
    });
}

}

用户模型:

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

class User extends Eloquent implements UserInterface, RemindableInterface {

/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'user';

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = array('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->password;
}

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

}

用户种子:

class UserSeeder extends DatabaseSeeder
{
public function run()
{
    $users = [
        [
            "username" => "ihkawiss",
            "password" => Hash::make("123456"),
            "email"    => "ihkawiss@domain.com"
        ]
    ];

    foreach ($users as $user)
    {
        User::create($user);
    }
}
}

数据库播种机:

class DatabaseSeeder extends Seeder {

/**
 * Run the database seeds.
 *
 * @return void
 */
public function run()
{
    Eloquent::unguard();

    $this->call('UserSeeder');
}

}

数据库配置:

return array(

/*
|--------------------------------------------------------------------------
| PDO Fetch Style
|--------------------------------------------------------------------------
|
| By default, database results will be returned as instances of the PHP
| stdClass object; however, you may desire to retrieve records in an
| array format for simplicity. Here you can tweak the fetch style.
|
*/

'fetch' => PDO::FETCH_CLASS,

/*
|--------------------------------------------------------------------------
| Default Database Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/

'default' => 'mysql',

/*
|--------------------------------------------------------------------------
| Database Connections
|--------------------------------------------------------------------------
|
| Here are each of the database connections setup for your application.
| Of course, examples of configuring each database platform that is
| supported by Laravel is shown below to make development simple.
|
|
| All database work in Laravel is done through the PHP PDO facilities
| so make sure you have the driver for your particular database of
| choice installed on your machine before you begin development.
|
*/

'connections' => array(

    'sqlite' => array(
        'driver'   => 'sqlite',
        'database' => __DIR__.'/../database/production.sqlite',
        'prefix'   => '',
    ),

    'mysql' => array(
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'laravel',
        'username'  => 'root',
        'password'  => '',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

    'pgsql' => array(
        'driver'   => 'pgsql',
        'host'     => 'localhost',
        'database' => 'database',
        'username' => 'root',
        'password' => '',
        'charset'  => 'utf8',
        'prefix'   => '',
        'schema'   => 'public',
    ),

    'sqlsrv' => array(
        'driver'   => 'sqlsrv',
        'host'     => 'localhost',
        'database' => 'database',
        'username' => 'root',
        'password' => '',
        'prefix'   => '',
    ),

),

/*
|--------------------------------------------------------------------------
| Migration Repository Table
|--------------------------------------------------------------------------
|
| This table keeps track of all the migrations that have already run for
| your application. Using this information, we can determine which of
| the migrations on disk have not actually be run in the databases.
|
*/

'migrations' => 'migrations',

/*
|--------------------------------------------------------------------------
| Redis Databases
|--------------------------------------------------------------------------
|
| Redis is an open source, fast, and advanced key-value store that also
| provides a richer set of commands than a typical key-value systems
| such as APC or Memcached. Laravel makes it easy to dig right in.
|
*/

'redis' => array(

    'cluster' => true,

    'default' => array(
        'host'     => '127.0.0.1',
        'port'     => 6379,
        'database' => 0,
    ),

),

);

希望有人可以在这里给我一个提示。

最好的问候 ihkawiss

最佳答案

在您的 CreateUserTable 迁移文件中,您必须使用 Schema::create 而不是 Schema::table

Schema::table 用于更改现有表,Schema::create 用于创建新表。

查看文档:

所以您的用户迁移将是:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUserTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('user', function(Blueprint $table) {
        {

            $table->increments("id",true);
            $table->string("username")->nullable()->default(null);
            $table->string("password")->nullable()->default(null);
            $table->string("email")->nullable()->default(null);
            $table->timestamps();

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists("user");
    }

}

关于php - 找不到 Laravel 4 迁移基表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19265202/

相关文章:

php - 同一台服务器上的两个 PHP 应用程序之间的通信?

mysql - Mysql日期比较,包括给定的日期范围

php - 为人对人禁用 Laravel 复数

php - 检查数组是否为空

php - 使用 pt-online-schema-change 是否可以在代码库部署的 "swap"阶段暂停?

mysql - 显示类别及其计数

mysql - 选择特定组合

sql - 如何在 Laravel 中进行 self 连接,从而产生不同的值

php - Laravel:具有本地范围的热切加载

php - 如何在循环中使用相同的变量