-
-
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
Nullable default value for Enum
types
#7221
Conversation
Fixed a bug with MySQL driver generating an invalid query for nullable `enum`s with a default value
Fixed a bug with Postgres driver generating an invalid query for nullable `enum`s with a default value
@@ -582,7 +582,7 @@ export class MysqlDriver implements Driver { | |||
normalizeDefault(columnMetadata: ColumnMetadata): string { | |||
const defaultValue = columnMetadata.default; | |||
|
|||
if ((columnMetadata.type === "enum" || columnMetadata.type === "simple-enum") && defaultValue !== undefined) { | |||
if ((columnMetadata.type === "enum" || columnMetadata.type === "simple-enum") && defaultValue != null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
!=
doesn't feel reliable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you think defaultValue !== undefined && defaultValue !== null
is more descriptive? cuz !=
is reliable, its just not as descriptive since we might have both these values here and each might mean a different thing in context.
I will change it if you think that it will help.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we could just place the null check before all the others? Then null
is always NULL
and the rest stays the same. Would apply the same fix for sets too.
if ((columnMetadata.type === "enum" || columnMetadata.type === "simple-enum") && defaultValue != null) { | |
if (defaultValue === null) { | |
return `NULL`; | |
} else if ((columnMetadata.type === "enum" || columnMetadata.type === "simple-enum") && defaultValue !== undefined) { |
what is the point to set |
true as I never saw one that doesn't automatically set the default to Regardless, the problem here with typeorm is not the usefulness of the |
from other side, it might be confusing if you put both nullable: true and default: null. Other programmer might ask you "why did you use both if nullable already does null? What is the point? (Like I did) Can I remove one?" Not sure what is the best approach here, maybe remove |
Well, I don't understand the other side's argument. So unless you do have this information, this confusion would never happen. But then if you have this knowledge, you can simply deduce that My understanding is that the TypeOrm core should not limit the TypeOrm should just do what someone expects from an ORM to do; translate the queries, map objects, and provide an easy way to work with a database. Reducing or adding to the meaning of messages is not the property of a good translator. |
All other column types (except 'set' which has the same problem) accept
You could extend this logic to something like
It should do what
I would almost have said that function calculateDefault() {
try {
return doSomething();
} catch (e) {
return null;
}
// ...
@Column({
default: calculateDefault() // never actually meant for this to be null
}) |
Description of change
This PR is the result of a conversation at #5371 regarding a bug with TypeOrm and MySQL resulting in the generation of an invalid table create query for enum fields with a default value of
null
This happens simply due to treating
null
as another value for the enum and therefore, escaping and passing it as a string to the database.The fix was easy, we are already checking for
undefined
before searching in enum and escaping the value in case there was no default value specified, now we are also checking fornull
in case the passed default value is special and should not be treated like others.This is done by simply replacing
value !== undefined
withvalue != null
therefore checking for both conditions.Pull-Request Checklist
master
branchnpm run lint
passes with this changenpm run test
passes with this changeFixes #0000
This is a small change and so I did the changes on my browser without setting up the project for test, build, and lint. But honestly, I don't think it can break anything! Feel free to reject it if it doesn't fits the standards!
Thank you for the great library.