dtgta48604 2018-02-08 18:35
浏览 60

外部数据库中的Laravel外键引用表?

I'm new to Laravel and databases in general. I'm writing a web application for student evaluations. I have an existing MySQL database that contains everything I need already; however, I am using Laravel's auth user table and trying to add a foreign key that references a Teacher table in the MySQL database. I keep getting the following error...

php artisan migrate:fresh

In Connection.php line 664:
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table users add constraint users_teacher_id_foreign foreign key (teacher_id) references p4j.teacher (teacher_id))

In Connection.php line 458: SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

My code is as follows...

config/database.php

'mysql' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DP_PORT', '3306'),
    'database' => env('DB_DATABASE', 'p4j'),
    'username' => env('DB_USERNAME', 'root'),
    'password' => env('DP_PASSWORD', '**mypassword**'),
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix' => '',
],

database/migrations/2014_10_12_000000_create_users_table.php

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->integer('teacher_id')->unsigned();
        $table->rememberToken();
        $table->timestamps();
    });

    Schema::table('users', function (Blueprint $table) {
          $table->foreign('teacher_id')->references('teacher_id')->on('p4j.teacher');
    });
}

app/User.php

protected $fillable = [
    'name', 'email', 'password', 'teacher_id'
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];
  • 写回答

2条回答 默认 最新

  • duanbi8089 2018-02-08 18:42
    关注

    Couple possible issues and solutions are that your db engine is not INNODB or loading order of migration files is incorrect. For the first solution you need to add $table->engine = 'InnoDB'; inside of Schema::create and if this doesn't fix the issue, i'll need you to post the names of your users and teacher migrations

    Possible fix:

    Both PRIMARY KEY constraint and UNIQUE constraint uses to enforce Entity integrity (defines a row as a unique entity for a particular table), but primary keys do not allow null values. Specifies the column that uniquely identifies a row in the table. The identified columns must be defined as NOT NULL.

    And since laravel increment function also

    Auto-incrementing UNSIGNED INTEGER (primary key) equivalent column.

    Simply removing ->default(null); and rerunning migrations should potentially fix the problem.

    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器