JS Knex:获取 table 的外键

JS Knex: getting foreign keys for a table

我想使用“knex”删除一些 table,但是当我尝试使用外键删除 table 时出现错误 Cannot delete or update a parent row: a foreign key constraint fails:

knex.schema.dropTableIfExists(name);

我可以使用 dropForeign() 函数删除外键,但我需要知道外键名称。

如何使用 'knex' 获取外键名称?

knex中常用的外键索引命名格式是:tableName_columnName_foreign.
例如:如果您在 table chat 中有一个名为 visitor_id 的外键,那么它的索引名称将是:chat_visitor_id_foreign

就是说,您不需要这个,除非有人明确覆盖了默认的外键名称。在这种情况下,请在迁移文件中搜索它或在数据库中查找它。