简体   繁体   English

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

[英]Laravel Foreign key Referencing table in External database?

I'm new to Laravel and databases in general. 我是Laravel和数据库的新手。 I'm writing a web application for student evaluations. 我正在编写一个用于学生评估的Web应用程序。 I have an existing MySQL database that contains everything I need already; 我有一个现有的MySQL数据库,其中包含我已经需要的所有内容。 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. 但是,我正在使用Laravel的auth用户表,并尝试在MySQL数据库中添加引用教师表的外键。 I keep getting the following error... 我不断收到以下错误...

php artisan migrate:fresh

In Connection.php line 664: 在Connection.php行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 )) SQLSTATE [HY000]:常规错误1215无法添加外键约束(SQL:ALTER TABLE users添加约束users_teacher_id_foreign外键( teacher_id )引用p4jteacherteacher_id ))

In Connection.php line 458: SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint 在Connection.php行458中:SQLSTATE [HY000]:常规错误:1215无法添加外键约束

My code is as follows... 我的代码如下...

config/database.php 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 数据库/迁移/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 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',
];

Couple possible issues and solutions are that your db engine is not INNODB or loading order of migration files is incorrect. 几个可能的问题和解决方案是您的数据库引擎不是INNODB或迁移文件的加载顺序不正确。 For the first solution you need to add $table->engine = 'InnoDB'; 对于第一个解决方案,您需要添加$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 Schema::create内部,如果这不能解决问题,我需要您发布用户名和教师迁移

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. PRIMARY KEY约束和UNIQUE约束都用于强制实体完整性(将行定义为特定表的唯一实体),但是主键不允许空值。 Specifies the column that uniquely identifies a row in the table. 指定唯一标识表中行的列。 The identified columns must be defined as NOT NULL. 标识的列必须定义为NOT NULL。

And since laravel increment function also 而且由于laravel 增量功能

Auto-incrementing UNSIGNED INTEGER (primary key) equivalent column. 自动递增UNSIGNED INTEGER(主键)等效列。

Simply removing ->default(null); 只需删除->default(null); and rerunning migrations should potentially fix the problem. 并且重新运行迁移可能会解决该问题。

You just need to make sure that the data type of the foreign key (p4j.teacher.teacher_id) corresponds to the same data type of the teacher_id column in your users table. 你只需要确保外键(p4j.teacher.teacher_id)的数据类型对应的数据类型相同teacher_id在用户表列。

We need to see the structure of the p4j.teacher table. 我们需要查看p4j.teacher表的结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM