不能在laravel创建表

时间:2014-10-16 07:52:36

标签: php mysql laravel laravel-4 artisan-migrate

我有这个问题,我不知道确切的迁移和种子数据库在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,
    ),

),

);

2 个答案:

答案 0 :(得分:1)

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

确保您的移植文件位于foo内,并且您之前无法进行任何迁移。

修改

如果您在默认文件夹中进行迁移,则应该只运行:

php artisan migrate

不使用--path="foo"

<强> EDIT2

你应该改变:

$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');

答案 1 :(得分:0)

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