-
Notifications
You must be signed in to change notification settings - Fork 286
Belongs to issue #7
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
Comments
I have fixed this issue by splitting it like this and using the unique property on column. Took me a while to find:
But now I am facing another issue. When trying to drop and sync the models, or simply using them, because of minification, I have to declare
It comes from
changing it to return |
Hey, thanks for contributing. As you already mentioned, this is the way to go: @AllowNull(false)
@ForeignKey(() => User)
@Column({
unique: true,
})
public userId: number;
@BelongsTo(() => User)
public user: User; Regarding your minification issue: It is not common practice to minify server-side code. Since there is no real benefit from it (see here and here). Sure, there CAN be a performance improvement - see here -, but when considering the 600 character limit it is not really necessary. Or have I overseen something? :) Anyway, if you want to make it work with minification, you have to add @Table({
tableName: 'Detail',
modelName: 'Detail'
} as DefineOptions<any>)
export class Detail extends Model<Detail> {
// ...
} For the next version I will add If you're happy with this answer, feel free to close this issue :) |
@RobinBuschmann Thanks for answering :) Regarding the unique and AllowNull, maybe we could add that example to the documentation if it makes sense ? Someone else might not pay attention (like I did) and might spend some time finding it out :) Maybe add the example at Regarding the minification: To give you an overview of why I decided to minify the code, I do it through webpack with Uglify plugin. The reason behind it is that the code will be deployed to raspberry pies that will be handed to clients and although you can still read the code doing reverse engineering, it is a bit discouraging. But the main reason is the performance gain (like you pointed out) since the raspberry pi will be controlling multiple micro services. Anyway, with or without minification some people might find another use for I have tried yesterday doing this:
but Typescript is complaining that the property |
@stelescuraul Since this is a sequelize issue I will not add this to the docs. I would like to keep only sequelize-typescript related stuff in the docs. As you can see here someone else faced the same problem with pure sequelize. Probably we can do some research on why sequelize doesn't implicitly add a In respect of minification: I do understand. Well that's fair enough. I'm just wondering that your assignment doesn't through an error. When I do this
I'm already getting an error: |
@RobinBuschmann Well, for a one-to-one relationship it would make sense to add a With this in mind, what i suggested regarding the docs, is to specify in sequelize-typescript how that relation should look like when it is defined (as an example). I have been looking for a decorator of type Regarding the edit: found these answers regarding the as and interface: |
@stelescuraul You mean, that the reason why Regarding TypeScripts structural typing: |
@RobinBuschmann Yes, that is what I mean. But from their documentation they have a
and then a
I have made a small test using the sequelize library with two models (User and Detail) with the following relation:
I have taken two approaches to add details to the User instance:
The resulting query is this:
As you can see, two details instances are referencing the same user instance when they shouldn't. But if I first create the two details instances and try to add one at the time to a user, this happens:
In this case, sequelize will update the first detail instance property So sequelize handles the creation and association differently (userInstance.createDetail vs userInstance.setDetail). I will be submitting a question to sequelize to check if that is the intentional behavior or if it is an actual bug. However, it can be avoided by setting the Thanks for the Typescript references. Also, glad to contribute to this project, I would help you with the code as well but I am still learning Typescript and I don't feel confident just yet :) Feel free to close this issue if you`d like. |
Thank you, I appreciate any help. |
@stelescuraul I've released a new version(0.3.0) with the issues you've discovered. thanks again |
Uh oh!
There was an error while loading. Please reload this page.
Say we have the following models :
Users:
Details:
The constrain in the database is:
That means that the same user can have multiple 'Details'
Easiest way to fix, is to support the constrain 'Unique' (maybe as an annotation) or have belongsToOne type of relation ?
@RobinBuschmann If i missed something, can you point me in the right direction on how to force the relation of HasOne and BelongsTo to only accept a one-to-one ?
The text was updated successfully, but these errors were encountered: