Closed boenrobot closed 4 years ago
The problem is that the generated
userId
inUserDetails
is a@RelationId
for theusers
table, but not a@Column
, meaning one can't insert into that table with an entity (only with a raw query). Furthermore, with lazy relations enabled, simply adding@Column
isn't quote going to cut it, as typeorm doesn't resolve the promise before using the ID.
You can still insert a row without using raw query. Take a look at this snippet:
const user = new Users();
user.username = 'username';
user.emailHmac = 'emailHmac'
const userRepo = typeorm.getRepository(Users);
await userRepo.save(user);
const details = new UserDetails();
details.email = "email"
details.password = 'password'
details.skypeAccount = 'skypeAccount'
details.user = Promise.resolve(user);
const userDetailsRepo = typeorm.getRepository(UserDetails);
const response = await userDetailsRepo.save(details);
Due to how typeorm lazy relation works you have to put user object into promise box.
Side-note: In typeorm@0.3.0 having one entity field for relation and primary key isn't allowed. Proper way is to generate two entity fields(@Column
and @OneToOne
) and mark column field as primary - such code is generated on @next
branch (#222).
I tried doing it in that way initially, but it didn't worked. TypeORM attempted to insert into user_details
without the user_id
column.
What you're saying is the proper way is what happens in #218 as well.
I'll try #222.
I've tested this code snippet and it worked correctly (typeorm@0.2.18).
Maybe you didn't await userRepo.save(user);
before trying to save details
?
I have two tables -
user
anduser_details
defined as:Important thing to note is that user_details.user_id is a non-nullable non-generated column that is the primary key for user_details, and is a foreign key that references users.user_id.
With the following options as part of
.torm-config
:The generated code is users.ts
user-details.ts
The problem is that the generated
userId
inUserDetails
is a@RelationId
for theusers
table, but not a@Column
, meaning one can't insert into that table with an entity (only with a raw query). Furthermore, with lazy relations enabled, simply adding@Column
isn't quote going to cut it, as typeorm doesn't resolve the promise before using the ID.Disabling relation IDs eliminates
userId
fromUserDetails
entirely, rather than create a@Column
in its place.I'm only just starting to work with TypeORM though, so I'm not sure what should the generator do... special casing such columns (somehow) to generate a single property decorated with both
@Column
and@RelationId
AND making the property non-lazy regardless of lazy setting works... except that whenever one inserts/updates aUserDetails
entity, there's also a (seemingly?) unnecesarry select for the relatedUser
from theusers
table.Adding a second property decorated with
@Column
, while keeping one only with@RelationId
would solve the above, but has its own problems, in particular, naming... Relation IDs would have to be subject to a naming strategy, in a separate method, and by default be named after the column with some prefix/suffix, to ensure it doesn't collide.Or perhaps OneToOne relations should be special cased, so that a
@Column
is generated with the ID regardless of@RelationId
setting, and@RelationId
is never to be generated for those (since the ID would be known by the@Column
).