我想将一个外键添加到名为"katalog"的表中.
ALTER TABLE katalog
ADD CONSTRAINT `fk_katalog_sprache`
FOREIGN KEY (`Sprache`)
REFERENCES `Sprache` (`ID`)
ON DELETE SET NULL
ON UPDATE SET NULL;
Run Code Online (Sandbox Code Playgroud)
当我尝试这样做时,我收到此错误消息:
Run Code Online (Sandbox Code Playgroud)Error Code: 1005. Can't create table 'mytable.#sql-7fb1_7d3a' (errno: 150)
INNODB状态出错:
120405 14:02:57表mytable的外键约束出错.#sql-7fb1_7d3a:
FOREIGN KEY (`Sprache`)
REFERENCES `Sprache` (`ID`)
ON DELETE SET NULL
ON UPDATE SET NULL:
Cannot resolve table name close to:
(`ID`)
ON DELETE SET NULL
ON UPDATE SET NULL
Run Code Online (Sandbox Code Playgroud)
当我使用此查询时它可以工作,但错误的"删除"操作:
ALTER TABLE `katalog`
ADD FOREIGN KEY (`Sprache` ) REFERENCES `sprache` (`ID` )
Run Code Online (Sandbox Code Playgroud)
两个表都是InnoDB,两个字段都是"INT(11)not null".我正在使用MySQL 5.1.61.尝试在MacBook Pro上使用MySQL …
我有一个MySQL数据库的备份脚本,使用mysqldump该--tab选项,因此它.sql为结构生成一个文件,并.txt为内容生成一个文件(管道分隔).
有些表有外键,所以当我导入它时,我收到错误:
第8行的错误1217(23000):无法删除或更新父行:外键约束失败
我知道使用SET FOREIGN_KEY_CHECKS=0(及SET FOREIGN_KEY_CHECKS=1之后).如果我将这些添加到每个.sql文件,那么导入工作.但显然在接下来的mysqldump那些被覆盖的人.
我也尝试将它作为一个单独的命令运行,如下所示,但错误回来了:
echo "SET FOREIGN_KEY_CHECKS=0" | mysql [user/pass/database]
[all the imports]
echo "SET FOREIGN_KEY_CHECKS=1" | mysql [user/pass/database]
Run Code Online (Sandbox Code Playgroud)
是否有其他方法可以在命令行上禁用FK检查?
我正在使用迁移将字段更改为nullable(),使用以下代码.
$table->integer('recipe_id')->nullable()->change();
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误.
SQLSTATE[HY000]: General error: 1025 Error on rename of './blackfisk/#sql-2
2d_a' to './blackfisk/preparations' (errno: 150 "Foreign key constraint is
incorrectly formed") (SQL: ALTER TABLE preparations CHANGE recipe_id recipe
_id INT DEFAULT NULL)
Run Code Online (Sandbox Code Playgroud)
我已经尝试使用外键设置为0
\DB::statement('SET FOREIGN_KEY_CHECKS=0');
Run Code Online (Sandbox Code Playgroud)
但它给出了同样的错误.当我尝试在Sequel Pro中运行查询时,我也会使用以下查询获得此错误.
SET FOREIGN_KEY_CHECKS = 0;
ALTER TABLE preparations CHANGE recipe_id recipe_id INT DEFAULT NULL;
SET FOREIGN_KEY_CHECKS = 1;
Run Code Online (Sandbox Code Playgroud)
任何想法如果我在这里遗失了什么?谢谢!
为什么 laravel schema 回复:
SQLSTATE[HY000]:一般错误:1005 无法创建表
employee_management。employees(errno: 150 "外键约束格式不正确") (SQL: alter tableemployees添加约束employees_city_id_foreign外键 (city_id) 引用city(id))PDOException::("SQLSTATE[HY000]: 一般错误: 1005 无法创建表
employee_management。employees(errno: 150 "外键约束格式不正确")")
我的桌子:
Schema::create('employees', function (Blueprint $table) {
$table->increments('id', true);
$table->string('lastname', 60);
$table->string('firstname', 60);
$table->string('middlename', 60);
$table->string('address', 120);
$table->integer('city_id')->unsigned();
$table->integer('state_id')->unsigned();
$table->integer('country_id')->unsigned();;
$table->foreign('city_id')->references('id')->on('city');
$table->foreign('state_id')->references('id')->on('state');
$table->foreign('country_id')->references('id')->on('country');
$table->char('zip', 10);
$table->integer('age')->unsigned();
$table->date('birthdate');
$table->date('date_hired');
$table->integer('department_id')->unsigned();
$table->integer('division_id')->unsigned();
// $table->integer('company_id')->unsigned();
$table->foreign('department_id')->references('id')->on('department');
$table->foreign('division_id')->references('id')->on('division');
// $table->foreign('company_id')->references('id')->on('company');
$table->string('picture', 60);
$table->timestamps();
$table->softDeletes();
});
Run Code Online (Sandbox Code Playgroud)
我的第二张城市表:
Schema::create('city', function …Run Code Online (Sandbox Code Playgroud)