简体   繁体   English

MariaDB 动态加入参考中的两个字段,这可能吗?

[英]MariaDB dynamic joined with two field in reference, is it possible?

Can we have sth like this in MariaDB?我们可以在 MariaDB 中有这样的东西吗?

ALTER TABLE `files` ADD
CONSTRAINT `fk_persons_cover`
    FOREIGN KEY (`foreign_key`, `model`)
    REFERENCES `persons` (`uuid`, "persons_cover")
    ON DELETE NO ACTION
    ON UPDATE NO ACTION;

I want refrence from files to persons table when files.uuid=persons.uuid and files.model="persons_cover"files.uuid=persons.uuidfiles.model="persons_cover"时,我想要从文件人员表的引用

Now with this code MariaDB said:现在用这个代码 MariaDB 说:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '"persons_cover")
    ON DELETE NO ACTION
    ON UPDATE NO ACTION' at line 4

Is there a solution?有解决办法吗?

Edit:编辑:

I know can use filelable super/interface table like this solution我知道可以像这个解决方案一样使用可filelable超级/接口表

Or use some auto generated fields in files table like this solution或者像这个解决方案一样在文件表中使用一些自动生成的字段

but they ar not good solution I think.但我认为它们不是很好的解决方案。

At first solution we can't find file 1 (row 1) is associated to what?在第一个解决方案中,我们找不到文件 1(第 1 行)与什么相关联? or can't find list of persons 1 (person row with pk = 1) files或找不到人员列表 1(pk = 1 的人员行)文件

At second solution we should add nullable foreign key field per new association!在第二种解决方案中,我们应该为每个新关联添加可为空的外键字段!

It should work, if the PERSONS table has a composite primary key (or unique key), encompassing uuid and persons_cover eg如果 PERSONS 表有一个复合主键(或唯一键),它应该可以工作,包括 uuid 和 people_cover 例如

create table persons( 
  uuid varchar( 100 ) 
, persons_cover varchar( 100 ) 
, constraint persons_pkey primary key ( uuid, persons_cover )
) ;
 
create table files (
  uuid varchar( 100 )
, model varchar( 100 )
) ;
 
alter table files
add constraint fk_persons_cover
foreign key( uuid, model ) references persons( uuid, persons_cover ) ;

If, in your PERSONS table, (only) the UUID column is the primary key column, the composite foreign key may not work.如果在您的 PERSONS 表中,(仅)UUID 列是主键列,则复合外键可能不起作用。 However, in this case, you could add a composite UNIQUE key to the PERSONS table (uuid and person_cover).但是,在这种情况下,您可以将复合 UNIQUE 键添加到 PERSONS 表(uuid 和 person_cover)。 Should the values stored in the table allow this, then you can add the composite foreign key to FILES.如果存储在表中的值允许这样做,那么您可以将复合外键添加到 FILES。

create table persons2( 
  uuid varchar( 100 ) 
, persons_cover varchar( 100 ) 
, constraint persons_pkey primary key ( uuid )  -- PK: only one column
) ;


-- does not work 
alter table files
add constraint fk_persons_cover2
foreign key( uuid, model ) references persons2( uuid, persons_cover ) ;

-- error
-- Failed to add the foreign key constraint. Missing index for constraint 'fk_persons_cover2' in the referenced table 'persons2'

 
-- add a composite unique key
alter table persons2 
add constraint upc_unique unique( uuid, persons_cover ) ;


-- no problem
alter table files
add constraint fk_persons_cover2
foreign key( uuid, model ) references persons2( uuid, persons_cover ) 
on delete no action
on update no action 
;

DBfiddle here . DBfiddle在这里

REFERENCES `persons` (`uuid`, "persons_cover")

No, you can't put a string literal in this line.不,您不能在此行中放置字符串文字。 You must name only column names that exist in the persons table.您必须仅命名persons 表中存在的列名。 Typically these are the columns of the primary key of that table.通常这些是该表的主键的列。

The foreign key constraint applies to all rows of your table.外键约束适用于表的所有行。 There's no such thing as a conditional foreign key that only applies on some rows.没有仅适用于某些行的条件外键之类的东西。

I understand you think the workarounds you linked to are not good solutions, but that should be a clue that polymorphic associations are not a good design.我知道您认为您链接到的解决方法不是好的解决方案,但这应该是多态关联不是一个好的设计的线索。

I wrote more about the disadvantages of polymorphic associations in numerous of my answers on Stack Overflow , and in a chapter of my book SQL Antipatterns, Volume 1 Avoiding the Pitfalls of Database Programming .在 Stack Overflow 上的许多答案以及我的书SQL Antipatterns, Volume 1 Avoiding the Pitfalls of Database Programming的一章中写了更多关于多态关联的缺点的信息。

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

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