我有一个应该使用 SymmetricDS 同步的数据库。有以下疑问,构建基础:
CREATE TABLE `defectstdreference` (
`std_doc_id` bigint(20) NOT NULL COMMENT 'Идентификатор НТД',
`improve_notice_doc_id` bigint(20) NOT NULL COMMENT 'Идентификатор предписания',
`defect_id` int(11) NOT NULL COMMENT 'Идентификатор дефекта',
`paragraph` varchar(4000) NOT NULL COMMENT 'Пункт НТД',
`reference_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'Идентификатор ссылки на пункт НТД',
`client_id` bigint(20) DEFAULT NULL,
PRIMARY KEY (`reference_id`),
KEY `Relationship70` (`std_doc_id`),
KEY `Relationship72` (`improve_notice_doc_id`,`defect_id`,`client_id`),
CONSTRAINT `Relationship70` FOREIGN KEY (`std_doc_id`) REFERENCES `std` (`doc_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `Relationship72` FOREIGN KEY (`improve_notice_doc_id`, `defect_id`, `client_id`) REFERENCES `improvementnoticedefect` (`doc_id`, `defect_id`, `client_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8 COMMENT='Ссылки на НТД выявленных нарушений';
以及引用的表格:
CREATE TABLE `improvementnoticedefect` (
`description` varchar(20000) NOT NULL COMMENT 'Описание нарушения',
`defect_id` int(11) NOT NULL COMMENT 'Номер нарушения в рамках предписания',
`doc_id` bigint(20) NOT NULL COMMENT 'Номер документа "Предписание"',
`client_id` bigint(20) NOT NULL,
PRIMARY KEY (`doc_id`,`defect_id`,`client_id`),
KEY `Relationship68` (`doc_id`,`client_id`),
CONSTRAINT `Relationship68` FOREIGN KEY (`doc_id`, `client_id`) REFERENCES `improvementnotice` (`doc_id`, `client_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Дефект';
如您所见,表
impovementnoticedefect
的主键中的列顺序和defectstdreference
的fk约束72中的列顺序是相同的。
然而,当同步开始时,SymmetricDS 给出了错误:
Cannot add foreign key constraint. Failed to execute: ALTER TABLE `defectstdreference`
ADD CONSTRAINT `Relationship72` FOREIGN KEY (`improve_notice_doc_id`, `defect_id`, `client_id`) REFERENCES `improvementnoticedefect` (`doc_id`, `defect_id`, `client_id`)
当我尝试转储 SymmetricDS 创建的表的 sql 时,我得到以下信息:
CREATE TABLE `improvementnoticedefect` (
`defect_id` int(11) NOT NULL,
`doc_id` bigint(20) NOT NULL,
`client_id` bigint(20) NOT NULL,
`description` varchar(20000) NOT NULL,
PRIMARY KEY (`defect_id`,`doc_id`,`client_id`),
KEY `Relationship68` (`doc_id`,`client_id`),
CONSTRAINT `Relationship68` FOREIGN KEY (`doc_id`, `client_id`) REFERENCES `improvementnotice` (`doc_id`, `client_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
可以看到,主键中字段的顺序是不同的。
我尝试手动运行错误的查询,它给出了错误,但是当我更改字段的顺序时,一切都很好。
我该如何解决这个问题?有什么我应该做的配置吗?我在这里真的很绝望,有人可以帮忙吗?
更新 我已经意识到,问题在于 SymmetricDS 使用 XML 发送表,而 PK 是从 XML 创建的:
<!DOCTYPE database SYSTEM "http://db.apache.org/torque/dtd/database">
<database name="dataextractor">
<table name="improvementnoticedefect">
<column name="defect_id" primaryKey="true" required="true" type="INTEGER" size="10">
<platform-column name="mysql" type="INT" size="10"/>
</column>
<column name="doc_id" primaryKey="true" required="true" type="BIGINT" size="19">
<platform-column name="mysql" type="BIGINT" size="19"/>
</column>
<column name="client_id" primaryKey="true" required="true" type="BIGINT" size="19">
<platform-column name="mysql" type="BIGINT" size="19"/>
</column>
<column name="description" required="true" type="VARCHAR" size="20000">
<platform-column name="mysql" type="VARCHAR" size="20000"/>
</column>
<foreign-key name="Relationship68" foreignTable="improvementnotice">
<reference local="doc_id" foreign="doc_id"/>
<reference local="client_id" foreign="client_id"/>
</foreign-key>
</table>
</database>
你可以看到,从这个XML主键是按照创建列的顺序生成的,这实际上是错误的