Laravel 4 迁移基础表未找到

23

我正在尝试跟随以下教程: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、Migration和DB配置。

CreateUserTable:

<?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");
    });
}

}

UserModel:

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;
}

}

UserSeeder:

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);
    }
}
}

DatabaseSeeder:

class DatabaseSeeder extends Seeder {

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

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

}

Database Config:

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,
    ),

),

);

Hope somebody can give me a hint here.

Best regards ihkawiss


我现在已经设法让它工作了,但不幸的是只能通过一个变通方法。我创建了一个名为user的表,添加了一个迁移未创建的列,运行php artisan migrate成功。之后我删除了创建的列。 - ihkawiss
我也遇到了同样的问题。migration:create使用的是"Schema::table",需要改为"Schema:create"。我认为这些模板可以在某个地方进行修改。 - jjwdesign
3个回答

92
在您的CreateUserTable迁移文件中,不要使用Schema::table,而是要使用Schema::createSchema::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");
    }

}

谢谢!对我很有用! - Arunabh Das

2
这可能是由于创建迁移的语法 php artisan migrate ... 不正确。在这种情况下,--create 将无法正确识别,并且你将看到 Schema::table 而不是预期的 Schema::create。如果像这样生成迁移文件,则还可能缺少一些用于创建迁移的其他标记,例如 $table->increments('id');$table->timestamps(); 例如,这两个命令将不会创建预期的创建表迁移文件:
php artisan migrate:make create_tasks_table --table="tasks" --create
php artisan migrate:make create_tasks2_table --table=tasks2 --create

然而,这个命令不会因为错误而失败。相反,laravel将使用Schema::table创建迁移文件。

当我创建新的迁移文件时,我总是使用完整的语法,例如:

php artisan migrate:make create_tasks_table --table=tasks --create=tasks 

为了避免类似问题的发生,请做好相应的预防措施。

1
有时是由自定义工匠命令引起的。其中一些命令可能会启动几个类。因为我们进行了回滚,所以无法找到表格。检查您的自定义工匠命令。您可以注释掉导致触发器的行。运行php artisan migrate命令,然后取消注释。这就是我所做的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接