我有两个表,table1是一个列ID的父表,table2是一个列IDFromTable1(不是实际的名称),当我把一个FK IDFromTable1到ID在table1,我得到的错误外键约束是不正确形成的错误。我想删除表2记录,如果表1记录被删除。谢谢你的帮助

ALTER TABLE `table2`  
   ADD CONSTRAINT `FK1` 
      FOREIGN KEY (`IDFromTable1`) REFERENCES `table1` (`ID`) 
      ON UPDATE CASCADE 
      ON DELETE CASCADE;

如果还需要其他信息,请告诉我。我是mysql的新手


当前回答

甚至我在mysql和libase上也遇到了同样的问题。 这就是问题所在: 要引用其他表的列的表在数据类型或数据类型大小方面都不同。

Error appears in below scenario:
Scenario 1:
Table A has column id, type=bigint
Table B column referenced_id type varchar(this column gets the value from the id column of Table A.)
Liquibase changeset for table B:

    <changeset id="XXXXXXXXXXX-1" author="xyz">
            <column name="referenced_id" **type="varchar"**>
        </column>
            </changeset>
    <changeSet id="XXXXXXXXXXX-2" author="xyz">
                <addForeignKeyConstraint constraintName="FK_table_A"
                    referencedTableName="A" **baseColumnNames="referenced_id**"
                    referencedColumnNames="id" baseTableName="B" />
    </changeSet>

Table A changeSet:

    <changeSet id="YYYYYYYYYY" author="xyz">
     <column **name="id"** **type="bigint"** autoIncrement="${autoIncrement}">
                    <constraints primaryKey="true" nullable="false"/>
                </column>
    </changeSet>

Solution: 
correct the type of table B to bigint because the referenced table has type bigint.

Scenrario 2:
The type might be correct but the size might not.
e.g. :
Table B : referenced column type="varchar 50"
Table A : base column type ="varchar 255"

Solution change the size of referenced column to that of base table's column size.

其他回答

甚至我在mysql和libase上也遇到了同样的问题。 这就是问题所在: 要引用其他表的列的表在数据类型或数据类型大小方面都不同。

Error appears in below scenario:
Scenario 1:
Table A has column id, type=bigint
Table B column referenced_id type varchar(this column gets the value from the id column of Table A.)
Liquibase changeset for table B:

    <changeset id="XXXXXXXXXXX-1" author="xyz">
            <column name="referenced_id" **type="varchar"**>
        </column>
            </changeset>
    <changeSet id="XXXXXXXXXXX-2" author="xyz">
                <addForeignKeyConstraint constraintName="FK_table_A"
                    referencedTableName="A" **baseColumnNames="referenced_id**"
                    referencedColumnNames="id" baseTableName="B" />
    </changeSet>

Table A changeSet:

    <changeSet id="YYYYYYYYYY" author="xyz">
     <column **name="id"** **type="bigint"** autoIncrement="${autoIncrement}">
                    <constraints primaryKey="true" nullable="false"/>
                </column>
    </changeSet>

Solution: 
correct the type of table B to bigint because the referenced table has type bigint.

Scenrario 2:
The type might be correct but the size might not.
e.g. :
Table B : referenced column type="varchar 50"
Table A : base column type ="varchar 255"

Solution change the size of referenced column to that of base table's column size.

我面临这个问题,当你把主键放在不同的数据类型,比如:

表1:

 Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->string('product_name');
        });

表2:

Schema::create('brands', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('brand_name');
        });

第二个表id的数据类型必须为增量

我在使用HeidiSQL时也遇到了同样的问题。您收到的错误非常神秘。我的问题是外键列和引用列的类型或长度不相同。

外键列是SMALLINT(5) UNSIGNED,引用列是INT(10) UNSIGNED。一旦我将它们都设置为完全相同的类型,外键的创建就会完美地工作。

即使字段名和数据类型相同,但排序规则不相同,也会导致该问题。

例如

资源描述 name | data 类型           | 排序 ActivityID           |         INT                         | latin1_general_ci ActivityID           |         INT                         | utf8_general_ci

试着把它变成

资源描述 name | data 类型           | 排序 ActivityID           |         INT                         | latin1_general_ci ActivityID           |         INT                         | latin1_general_ci

....

这对我很管用。

我在Symfony 2.8上也遇到了同样的问题。

我一开始没有得到它,因为外键的int长度等没有类似的问题。

最后,我必须在项目文件夹中执行以下操作。(服务器重启不起作用!)

应用程序/控制台学说:缓存:clear-metadata 应用程序/控制台学说:缓存:clear-query 应用程序/控制台学说:缓存:明确的结果