Search code examples
mariadbnestjstypeormnestjs-typeorm

TypeORM - Foreign key without constraint and reference


I'm developing a backend app using Nest.js with an existing MariaDB database and I'm using TypeORM. I can't alter the production database table but can read from it. So I have separate development DB where my TypeORM is syncing entities and creating tables.

I'm trying to create the exact table DDLs in my DB as in the production DB.

One of the tables has a KEY but without CONSTRAINT.

DDL:

CREATE TABLE `documents` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) DEFAULT NULL,
  `content_type` varchar(256) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

As you can see there is KEY user_id (user_id) defined but no constraints. Is it possible in TypeORM to define only a key, but without a constraint and a reference?


Solution

  • Support for this has been implemented since 2021 via the createForeignKeyConstraints decorator option, see: