我正在尝试在我的列中分配大整数,但它不允许我定义关系,除非我将其更改为标准整数。没什么大不了的,但有兴趣知道为什么这会给我带来迁移错误。
移民(工作)
public function up()
{
//create the table
Schema::create(self::TBL_eportfoliouservalues, function($table){
$table->engine = 'InnoDB';
$table->increments('id');
$table->integer('user')->unsigned()->index();
$table->integer('document')->unsigned()->index();
$table->integer('section')->unsigned()->index();
$table->integer('element')->unsigned()->index();
$table->integer('parent');
$table->text('value');
// foreign indexes
$table->foreign('user')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('document')->references('id')->on('eportfoliodocuments')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('section')->references('id')->on('eportfoliosections')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('element')->references('id')->on('eportfolioformelements')->onDelete('cascade')->onUpdate('cascade');
});
}
移民(不工作)
public function up()
{
//create the table
Schema::create(self::TBL_eportfoliouservalues, function($table){
$table->engine = 'InnoDB';
$table->increments('id');
$table->bigInteger('user')->unsigned()->index();
$table->bigInteger('document')->unsigned()->index();
$table->bigInteger('section')->unsigned()->index();
$table->bigInteger('element')->unsigned()->index();
$table->bigInteger('parent');
$table->text('value');
// foreign indexes
$table->foreign('user')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('document')->references('id')->on('eportfoliodocuments')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('section')->references('id')->on('eportfoliosections')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('element')->references('id')->on('eportfolioformelements')->onDelete('cascade')->onUpdate('cascade');
});
}
错误
[Exception]
SQLSTATE[HY000]: General error: 1005 Can't create table 'drillers.#sql-1a4_2c2' (errno: 150) (SQL:
alter table `eportfoliouservalues` add constraint eportfoliouservalues_user_foreign foreign key (
`user`) references `users` (`id`) on delete cascade on update cascade) (Bindings: array (
))
答案 0 :(得分:4)
如何在其他表上定义ID?如果您使用$table->increments('id')
,那么将创建integer
类型的列,并且由于外键字段必须与另一个表上的主键字段匹配,您将无法创建关系
解决这个问题的方法是在其他表格上使用$table->bigIncrements('id')
。
答案 1 :(得分:0)
如果tableOne迁移中的主键(默认为)为BigInteger 因此,tableTwo迁移中的外键应为 bigInteger 和 unsigned()函数。
例如代码。
tableOne的迁移
public function up()
{
Schema::create('tableOne', function (Blueprint $table) {
$table->bigIncrements('id');
}
表Two的迁移
public function up()
{
Schema::create('tableTwo', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('fk_id')->unsigned()->nullable() ;
$table->bigInteger('fk_id')->default(20)->change();
$table->foreign('fk_id')->references('id')->on('tableOne');
}
希望这对某人有帮助。
答案 2 :(得分:0)
聚会晚了,但我认为应该在某个地方分享。
这是一个非常有效的升级,没有得到充分的记录。该类型需要匹配ID和外键。
此脚本输出您需要的迁移。
使用备份的数据库,后果自负。进行迁移,并将以下内容添加到up()中。将down()留空,以便您可以根据需要进行回滚和调整。它实际上不会做任何事情,但是会输出需要做的事情。对看到的内容满意后,您可以将输出粘贴到迁移文件中。
如果您取消对回声的注释,则可以查看所查看的内容以及确定的需求发生变化。
$tableName = $table->Tables_in_yourdatabasename; //eg Tables_in_myproject
在下面的代码中修改上面的行
$tables = \DB::select('SHOW TABLES');
// this just to get the tabs right in sublime;
$i = 0;
foreach($tables as $table) {
// ### Change yourdatabasename below ###
$tableName = $table->Tables_in_yourdatabasename;
// echo "#######################\n";
// echo '########## '.$tableName."##########\n\n";
$columns = \DB::select('show columns from ' . $tableName);
foreach ($columns as $value) {
if((substr($value->Field, -2) === 'id') && (substr($value->Type,0,3) === "int" || substr($value->Type,0,6) === "bigint")) {
// echo "$value->Field : type is '$value->Type'\n" ;
if(substr($value->Type,0,6) === "bigint") {
// echo "No Change needed\n";
}
if(substr($value->Type,0,3) === "int") {
// echo "To Update $value->Field to 'BIGINT':\n\n";
if($i === 0) {
echo 'Schema::table(\''.$tableName.'\', function (Blueprint $table) {'."\n";
$i++; //only matters for the first row. For the tabs.
}else{
echo "\t\t".'Schema::table(\''.$tableName.'\', function (Blueprint $table) {'."\n";
}
// maintain the auto increment
if($value->Field === 'id') {
echo "\t\t".' $table->bigIncrements(\'id\')->change();'."\n\t\t";
}else{
echo "\t\t".' $table->bigInteger(\''.$value->Field.'\')->unsigned()->change();'."\n\t\t";
}
echo '});'."\n";
}
}
}
// echo "\n\n";
}
请小心并仔细检查输出,因为您可能不想更改所有表。例如,我会跳过迁移表,因为我怀疑您会引用它。
以防我不说。首先备份您的数据库。祝你好运