Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to disable foreign keys creation #3120

Closed
alfaproject opened this issue Nov 16, 2018 · 41 comments · Fixed by #7277, mattwelke/typeorm-postgres-example#165 or newerton/gobarber-2-backend#17

Comments

@alfaproject
Copy link
Contributor

alfaproject commented Nov 16, 2018

Issue type:

[x] question
[ ] bug report
[x] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[x] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[x] latest
[ ] @next
[ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

Is there a way to disable foreign keys creation? I searched a bit and couldn't find it.

If not, is there a chance such option can be implemented? Maybe I can give it a try if you point me in the right direction. (:

@Kononnable
Copy link
Contributor

Adding @pleerock
I'm not a fan of having relational database without foreign keys, but I'm concerned if introducing such change could break some of the functionalities like onUpdate, onDelete on relations.

@pleerock
Copy link
Member

pleerock commented Jan 7, 2019

Yeah it can bring some issues. I would like to know what is the point of this feature request.

@alfaproject
Copy link
Contributor Author

Our DBA told us to remove all foreign keys for performance reasons and to ease sharding, clustering and online migrations. It's actually the second company I work for where this was requested, but it's my first time using TypeORM, so I had to fork and get rid of the line that creates the foreign keys.

I actually don't see the point of enforcing them. It seems to me like it's up to the application to maintain consistency its own way or to use foreign keys where appropriate if desired, right? (The option is there if anyone wants to use them.)

@pleerock
Copy link
Member

pleerock commented Jan 8, 2019

Think we can try to do it, but I'm not sure about all consequences

@alfaproject
Copy link
Contributor Author

That would be awesome. Maybe I can help if I find some free time. A bit over-worked right now ):

@Diluka
Copy link
Contributor

Diluka commented Jan 24, 2019

Oh, this is what we discussed in the beginning #1031

@vlapo
Copy link
Contributor

vlapo commented Jan 26, 2019

As @rudism requested in #3382. It would be nice to have option to disable FK creation only for specific relation too.

@exeleon
Copy link

exeleon commented Feb 7, 2019

This feature would let to have "soft" relations across databases in different hosts. That is really my requirement now.

@ITJesse
Copy link

ITJesse commented Jun 21, 2019

Maybe we should give user an option to choose whether to use a foreign key or not.

@david-eos
Copy link

Any news on this? Is it possible to disable it?

@brianschardt

This comment has been minimized.

@eddieajau
Copy link

I have had a similar problem. The issue I had is with my unit testing where I use synchronise. My production code would be fine, but TypeORM wants to put the FK on the wrong table. In the example below I put the relation on the stats table, not the channel table.

In case it helps anyone else, I've just used leftJoinAndMapOne to work around this problem.

@Entity('channel')
export class Channel {
  @PrimaryGeneratedColumn()
  public id: number;

  // ...
  public stats?: ChannelStats;
}

@Entity('channel_stats')
export class ChannelStats {
  @PrimaryColumn()
  public channel_id: number;

  @Column({ unsigned, default: 0 })
  public members: number;
}

// Then something like
const users = await connection
    .getRepository(Channel)
    .createQueryBuilder("c")
    .leftJoinAndMapOne('c.stats', 'channel_stats', 's', 's.channel_id = c.id')
    .getMany();

@frankra
Copy link

frankra commented Oct 4, 2019

@alfaproject can you please share the changes you did? I am having some super weird issues atm and I think they might be related to the auto-creation of FKs. Thanks!

@alfaproject
Copy link
Contributor Author

Basically created a patch to comment out the method that creates the relations.

I've stopped using the library, so can't remember the exact file, so you have to search for it.

@chengjianhua

This comment has been minimized.

@alfaproject

This comment has been minimized.

@JohnnyDevNull

This comment has been minimized.

@jituanlin
Copy link

config with synchronize: false will global disable auto synchronize.

@insanehong
Copy link

Our DBA told us to remove all foreign keys for performance reasons too.
I need option can be disable FK creation

@ruichenx

This comment has been minimized.

@v1d3rm3
Copy link
Contributor

v1d3rm3 commented Apr 20, 2020

It could be useful in tests cases too.

@truongezgg

This comment has been minimized.

@guoguo0202

This comment has been minimized.

2 similar comments
@herenickname

This comment has been minimized.

@tienne

This comment has been minimized.

@imnotjames
Copy link
Contributor

Or let's hope we get some new developments on this topic :) I would be willing to submit PR to allow the user to disable FK validation, but I tried once to submit a PR and I didn't get a response...

The PR is still up? Happy to review it.

@cantremember
Copy link

we are sort of "constrained" in our schema as well by the enforcement of foreign key constraints. here's another (recent) Issue asking for a way to tell TypeORM not to auto-generate one

#5977

@alpharder
Copy link
Contributor

I've added the #7112 PR but I don't have enough time to write the tests ATM, can someone assist me in that?

Saniol added a commit to Saniol/typeorm that referenced this issue Jan 14, 2021
This new option allows to create relation without using foreign keys

Closes: typeorm#3120
@Saniol Saniol mentioned this issue Jan 14, 2021
7 tasks
Saniol added a commit to Saniol/typeorm that referenced this issue Jan 18, 2021
@vahidvdn
Copy link

Any update on this? I must use an old db which I need to disable the foreign key constraint.

pleerock pushed a commit that referenced this issue Feb 8, 2021
* Add relation option "createForeignKeyConstraints"

* feat: add createForeignKeyConstraints relation option

This new option allows to create relation without using foreign keys

Closes: #3120

* test: add cases for createForeignKeyConstraints relation option

* docs: createForeignKeyConstraints relation option

* test: remove .only

* chore: remove unused test files

* test: add case checking if relation without foreign key works as expected

* fix: register join columns for relation without foreign key

Closes: #3120

Co-authored-by: Alexander Bolshakov <alexanderb@cubedmobile.com>
@aacotroneo
Copy link

as a closing comment #7277 was merged to master (check releases for the needed version): you can now do this to disable the FK by

 @ManyToOne(type => Person, {
        createForeignKeyConstraints: false
    })

@mrtoorich
Copy link

https://typeorm.io/#/relations-faq/avoid-foreign-key-constraint-creation: the doc, avoid foreign key constraint creation.

@mrtoorich
Copy link

mrtoorich commented Dec 20, 2021

Is there a way, that setting createForeignKeyConstraints: false globally? Thanks! 🤔

@ofirgellerSM
Copy link

How can I do this when using EntitySchemas?

Adding createForeignKeyConstraints:false to the relation definition does not seem to work (and the type does not have this property)

@bryan-gc
Copy link

Is there a way, that setting createForeignKeyConstraints: false globally? Thanks! 🤔

Did someone found a way to do this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment