question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to give foreign key reference of a table in another database?

See original GitHub issue

Environment

Knex version: “^1.0.1” Database + version: MySql 8.0.23 OS: Ubuntu 20.04

Select applicable template from below. @smorey2

Feature discussion / request

  1. Explain what is your use case: I have two databases (sso, and items). I wish to create a migration to create a new table belonging to items database that have foreign keys referencing to a table(eg. users) in sso database.

I am using:

table.integer('user_id').unsigned();
table.foreign('user_id').references('sso.users.id');

This produce the error: migration failed with error: alter table `group_sale_items` add constraint `group_sale_items_user_id_foreign` foreign key (`user_id`) references `sso`.`users` (`id`) - Referencing column 'user_id' and referenced column 'id' in foreign key constraint 'group_sale_items_user_id_foreign' are incompatible.

  1. Explain what kind of feature would support this: It would be great to have a feature where we can specify the database too like we specify the table using inTable(‘users’).

  2. Give some API proposal, how the feature should work: An API to which we can give database name and it will reference the table to that particular database’s table.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
santialbocommented, Mar 14, 2022

I don’t know how to do cross-db references but with knex.raw you can pass any sql you want on it. I have no idea if it will work for your usecase.

export async function up(knex) {
  await knex.raw("your sql here");
}

By the way, this is not a knex issue.

0reactions
ritikbangercommented, Mar 14, 2022

@smorey2 I am using different knex configurations for different databases. I have a docker container for mysql dbs and I am giving cross-db references in my mysql dump, it do check for tables constraint and work well in the case. It is something similar to operate multiple microservices together.

@santialbo Could you give a specific example of using knex.raw in migration files for giving cross-db references so that it can check into a particular db and put up the constraint in another db.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add Foreign Key relationship between two Databases
The short answer is that SQL Server (as of SQL 2008) does not support cross database foreign keys--as ...
Read more >
How to set foreign key in table in one database that references ...
A FOREIGN KEY constraint cannot reference a table in a different database. It's by design. Here's an example: CREATE DATABASE test1 GO USE ......
Read more >
Create Foreign Key Relationships - SQL Server | Microsoft Learn
FOREIGN KEY constraints can reference another column in the same table, and is referred to as a self-reference. A FOREIGN KEY constraint ......
Read more >
SQL Server Referential Integrity Across Databases Using ...
Maybe you ask yourself why Microsoft does not offer the possibility to create a foreign key amongst different databases or even different ......
Read more >
What is a foreign key? (With SQL examples) - Cockroach Labs
Foreign keys link data in one table to the data in another table. A foreign key column in a table points to a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found