1

我有这个问题,我不知道在 laravel (php artisan) 中工作的迁移和种子数据库,我有这个模型:

use Illuminate\Auth\UserInterface;


class User extends Eloquent implements UserInterface {
    public function getAuthIdentifier() {
        return $this->getKey();
    }
    public function getAuthPassword() {
        return $this->password;
    }
    public function cats(){
        return $this->hasMany('Cat');
    }
    public function owns(Cat $cat){
        return $this->id == $cat->owner;
    }
    public function canEdit(Cat $cat){
        return $this->is_admin or $this->owns($cat);
    }
    public function getRememberToken(){
        return $this->remember_token;
    }
    public function setRememberToken($value){
        $this->remember_token = $value;
    }
    public function getRememberTokenName(){
        return 'remember_token';
    }

}

这是我的迁移文件:

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

class CreateUser extends Migration {

    public function up()
    {
        Schema::create('users', function($table){
            $table->increments('id');
            $table->string('username');
            $table->string('password');
            $table->boolean('is_admin');
            $table->timestamps();
        });

        Schema::table('cats', function($table){
            $table->integer('user_id')->nullable()->references('id')->on('users');
        });
    }


    public function down()
    {
        Schema::table('cats', function($table){
            $table->dropForeign('cats_user_id_foreign');
            $table->dropColumn('user_id');
        });
        Schema::drop('users');
    }

}

最后是我的播种机代码:

class UsersTableSeeder extends Seeder {
    public function run(){

        DB::table('users')->delete();

        User::create(array(
            'username' =>'admin', 'password' =>Hash::make('hunter2'), 'is_admin' =>true
        ));

        User::create(array(
           'username'=>'scott', 'password' =>Hash::make('tiger'), 'is_admin' => false
        ));
    }
}

当我尝试迁移时,我收到了这条消息:

$php artisan migrate --path="foo"

Nothing to migrate

然后当我尝试:

$php artisan db:seed

这条消息显示给我:

[PDOException]
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel.users' doesn't exist

请帮助我,谢谢你:)

和 config/database.php:

return array(
'fetch' => PDO::FETCH_CLASS,
'default' => 'mysql',
'connections' => array(

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

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

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

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

),


'migrations' => 'migrations',



'redis' => array(

    'cluster' => false,

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

),

);
4

2 回答 2

1

似乎在目录中foo您没有任何迁移文件,或者您已经启动了一些迁移(您是否使用来自 2 个目录的迁移?)。

确保你有foo你的迁移文件,并且你之前没有运行任何迁移。

编辑

如果您在默认文件夹中有迁移,您应该简单地运行:

php artisan migrate

不使用--path="foo"

编辑2

你可能应该改变:

$table->integer('user_id')->nullable()->references('id')->on('users');

进入:

$table->dropColumn('user_id');
$table->integer('user_id')->unsigned()->nullable();
$table->foreign('user_id')->references('id')->on('users');
于 2014-10-16T08:02:01.217 回答
0

可能是当您第一次尝试迁移时,它会将迁移记录到您的迁移表中。检查你的迁移表,让我知道你看到了什么。

于 2014-10-16T08:20:00.677 回答