-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
feat: make association definition methods throw if the generated foreign key is incompatible with an existing attribute #14715
base: main
Are you sure you want to change the base?
Conversation
There's some failing integration tests. And apparently postgres does a much better stacktrace than the other dialects, that might be something to look into in the future |
The difference is the quality of error messages is caused by mocha. We use the native mocha doesn't support .cause yet, so in our CI the error message is still missing a lot of information even though node supports it |
Looks like I have to draft this PR until #14687 is complete. This change results in ALTER TABLE "testSyncs"
ALTER COLUMN "age" SET DATA TYPE VARCHAR(255)
ALTER COLUMN "age" DROP NOT NULL The issue here is that for some reason, db2 will not allow you to modify the same column twice in the same alter statement. It rejects the query with the message
That is something that will need to be fixed by that other PR |
@ephys Could you perhaps add a review or such to my Mocha PR to give it a bit more weight and push it forward to a possible inclusion for the benefit of us all? :) |
Pull Request Checklist
yarn test
oryarn test-DIALECT
pass with this change (including linting)?Description Of Change
This PR normalizes the
allowNull
andprimaryKey
options. At least it's the initial goal. 😅Association Breaking change
That change lead to this test failing:
This fails because the attributes passed to
sequelize.define
take priority over the attributes added by association definition methods. In this case,allowNull: false
is ignored because it's already been defined bysequelize.define
.This is a good change IMO. If an attribute has already been defined, the association definition method should not be able to completely change its properties. Users should write this instead:
In order to catch this and warn the user, association definition methods will now throw an error if the foreign key they are adding is incompatible with a previously-defined attribute. with the following message:
This change is slightly annoying because
belongsToMany
associations will attempt to create two non-nullable foreign keys by default. But this is only an issue if the user also created the foreign keys on the through model, and is fixed by setting theirallowNull
option to false.Polymorphic Association Breaking change
This change lead to discovering another bug, with polymorphic associations.
In the above example, the two
belongsToMany
try to use the same foreign key to reference two different models. Before this PR,ItemTag.getAttributes().taggable_id.references
would point toPost
(notComment
).After this PR, the code throws because the second
belongsToMany
call tries to maketaggable_id
referenceComment
, but it cannot as it already referencesPost
This is imo a good change to avoid unexpected behaviors.
The solution is simple: use
foreignKeyConstraints: false
on one or bothbelongsToMany
Commit message