-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
ManyToOne relation without foreign key constraint possible? #3382
Comments
I'm not sure I understand what is exactly your problem. You don't want to create FK constraint but FKs don't always enforce existence of a corresponding rows. It should work as you like if column is nullable. |
The problem is that I want to be able to put a value in the column that doesn't necessarily have a corresponding row in the foreign table, which wouldn't be permitted by a foreign key constraint. As a (lame) real-life example, imagine tracking a one-to-many relationship between people and their favorite colors: table
table
The idea would be to define the relationship between |
We already have a feature request about disabling FK option. Closing this issue in favour of #3120. |
In my case it worked for me to add synchronization = false to the entity header. |
@JPerkan (and anyone else who came here via Google, as this issue seems to have a better search ranking than the one with the fix): Foreign keys can be directly disabled on individual relations per the comment here: #3120 (comment) First available in version 0.2.31 (2021-02-08) |
@LachlanStuart I come from google and I very much appreciate your comment. 🙏 |
Issue type:
[x] question
[ ] bug report
[ ] feature request
[ ] documentation issue
Database system/driver:
[ ]
cordova
[ ]
mongodb
[ ]
mssql
[ ]
mysql
/mariadb
[ ]
oracle
[x]
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:
I am trying to establish a relationship between two entities like these:
There may or may not be an entry in
EntityBInformation
for anentityBId
that appears in theRelationship
table, and theRelationship
table could contain multiple rows with the sameentityBId
. My goal is to make it so that I can either lazy or eager load the appropriateEntityBInformation
intoRelationship
, leaving it null if there isn't one for thatentityBId
.When I set up a typical
ManyToOne
andOneToMany
relation between the tables like this:This creates a foreign key constraint from the
Relationship
table to theEntityBInformation
table, which is not what I want because then in order for theRelationship
to exist there must be anEntityBInformation
row that corresponds first. My goal is to have this property loadable through the repository API, but without that foreign key restraint so thatentityBInformation
could be null if there's no corresponding row to populate it.Am I barking up the wrong tree trying to define a relation here? The only other option I can see is to create the
entityBInformation
property onRelationship
and then manually doleftJoinAndMapOne
with the query builder every time (losing some of the convenience of the repository API).The text was updated successfully, but these errors were encountered: