Laravel 5使用PostgreSQL进行数据库播种

时间:2018-05-04 04:27:22

标签: php postgresql laravel-5

我有桌面用户和桌面帖子当代码在MySQL环境下运行时一切正常,但是当我尝试将它们部署到heroku(Postgre SQL)时,问题就像下面的图片一样,我在这里想念一下吗?提前谢谢:

Error picture

这是我的用户表: 的 1.2014_10_12_000000_create_users_table.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');
    }
}

这是我的帖子表: 的 2。 2018_05_03_183219_create_posts_table.php

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

这是我的 UsersTableSeeder.php

<?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')
            ],
        ]);
    }
}

这是我的 PostsTableSeeder.php

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

这里我的最终代码是 DatabaseSeeder.php

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

1 个答案:

答案 0 :(得分:0)

根据Pavel Stehule here

&#34; PostgreSQL不支持任何配置选项&#34;

尝试使用disable trigger