Php 使用PostgreSQL的Laravel 5数据库种子设定

Php 使用PostgreSQL的Laravel 5数据库种子设定,php,postgresql,laravel-5,Php,Postgresql,Laravel 5,我有表用户和表帖子,当代码在MySQL环境下运行时一切都正常,但是当我试图将它们部署到heroku(PostgreSQL)中时,问题如下图所示,我是否遗漏了什么?提前感谢: 以下是我的用户表: 1.2014\u 10\u 12\u000000\u创建用户\u表格.php <?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Databas

我有表用户和表帖子,当代码在MySQL环境下运行时一切都正常,但是当我试图将它们部署到heroku(PostgreSQL)中时,问题如下图所示,我是否遗漏了什么?提前感谢:

以下是我的用户表: 1.2014\u 10\u 12\u000000\u创建用户\u表格.php

<?php

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}
<?php

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

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('restrict');
            $table->string('title');
            $table->string('slug')->unique();
            $table->text('excerpt');
            $table->text('content');
            $table->string('image')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}
<?php

use Illuminate\Database\Seeder;

class UsersTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // Mengosongkan tabel users
        // DB::statement('SET FOREIGN_KEY_CHECKS=0');

        // Membuat 3 data users
        DB::table('users')->insert([
            [
                'name' => 'Ogi',
                'email' => 'ogi@example.com',
                'password' => bcrypt('secret')
            ],
            [
                'name' => 'John Doe',
                'email' => 'johndoe@example.com',
                'password' => bcrypt('secret')
            ],
            [
                'name' => 'Jane Doe',
                'email' => 'janedoe@example.com',
                'password' => bcrypt('secret')
            ],
        ]);
    }
}
<?php

use Illuminate\Database\Seeder;
use Faker\Factory;
use Illuminate\Support\Facades\Schema; // Do not forget to import this namespace


class PostsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Schema::disableForeignKeyConstraints(); // This is the answer
        // Mengosongkan tabel posts
        DB::table('posts')->truncate();
        Schema::enableForeignKeyConstraints();  // This is the answer

        // Generate 10 data secara acak menggunakan faker library
        $posts = [];
        $faker = Factory::create();

        for($i=1 ; $i <= 10 ; $i++) {

            $image = "Post_Image_" . rand(1, 5) . ".jpg";
            $date = date("Y-m-d H:i:s", strtotime("2018-05-04 00:00:00 +{$i} days"));

            $posts[] = [
                'user_id' => rand(1, 3),
                'title' => $faker->sentence(rand(5, 9)),
                'excerpt' => $faker->text(rand(70, 150)),
                'content' => $faker->paragraphs(rand(5, 9), true),
                'slug' => $faker->slug(),
                'image' => rand(0, 1) == 1 ? $image : NULL,
                'created_at' => $date,
                'updated_at' => $date,
            ];
        }

        DB::table('posts')->insert($posts);
    }
}
<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $this->call(UsersTableSeeder::class);
        $this->call(PostsTableSeeder::class);
    }
}
符合Pavel Stehule的

“PostgreSQL不支持任何配置选项”


试试看

我从中得到了最好的答案