【问题标题】:Create two table with laravel migration with references使用带有引用的 laravel 迁移创建两个表
【发布时间】:2016-03-25 23:38:23
【问题描述】:

我需要创建两个表: 先articles

public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->string('title');
            $table->text('body');
            $table->timestamps();
            $table->timestamp('roba_spremna');
            $table->timestamp('auction_end');
            $table->string('key');

            $table->foreign('user_id')
                  ->references('id')
                  ->on('users')
                  ->onDelete('cascade');
        });
    }

然后offers:

 public function up()
    {
        Schema::create('offers', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->integer('article_id');
            $table->integer('price');
            $table->string('comment');
            $table->timestamps();

            $table->foreign('user_id')
                ->references('id')
                ->on('users')
                ->onDelete('cascade');

            $table->foreign('article_id')
                ->references('id')
                ->on('articles')
                ->onDelete('cascade');
        });
    }

当我运行php artisan migrate 时,我得到:

有什么问题?为什么我不能成功创建这两个表?

【问题讨论】:

  • 您的数据库中已经存在该表...手动检查您的数据库..

标签: php laravel reference migration laravel-artisan


【解决方案1】:

这是因为表已经创建。删除表并重新运行。

【讨论】:

  • 我尝试删除表并重新运行迁移,但我得到:PS PS C:\wamp\www\learn5> php artisan migrate:refresh 回滚:2014_10_12_100000_create_password_resets_table 回滚:2014_10_12_000000_create_users_table [Illuminate\Database\ QueryException] SQLSTATE[HY000]: 一般错误: 1215 Impossible d'ajouter des contraintes d'index externe (SQL: alter table offers add constraint offers_article_id_foreign foreign key (article_id) references articles (id) on delete cascad e) [PDOException] SQLSTATE[HY000]: 一般错误:1215Impossible d'ajouter des contraintes d'index externe
  • 看来您需要删除整个数据库并重新取消。
【解决方案2】:

如果你想引用一个表的id列,你的列需要是无符号的,offers表中的article_id列不是无符号的,使其无符号并刷新迁移

【讨论】:

    猜你喜欢
    • 2020-03-07
    • 2021-01-30
    • 2016-07-04
    • 2015-01-14
    • 2023-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-22
    相关资源
    最近更新 更多