keystonejs / keystonejs/keystone

Default value automatically set for MySQL TEXT field

Open
#8,333 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
10k
Forks
1.3k
Avg merge
19h 14m
Merged PRs (30d)
19

Description

I want to create a text field wich allows more than 191 charracters. So when i create a textfield and change its native type to be a MySQL TEXT field. The following error occurs upon running the migration due to setting the field property `isNullable` to `false`.

`Database error:
BLOB, TEXT, GEOMETRY or JSON column 'coverText' can't have a default value`

The database model:
```typescript
coverText: text({
validation: {
isRequired: true,
length: {
min: 1,
max: 1000
}
},
db: {
nativeType: 'Text',
isNullable: false,
},
ui: { displayMode: 'textarea' }
}),
```

In the sourcecode I can see that when the property isNullable is set, a `defaultValue` of `''` will be set in the following line:
```typescript
const defaultValue = isNullable === false || _defaultValue !== undefined ? _defaultValue || '' : undefined;
```
https://github.com/keystonejs/keystone/blob/e18659a78bdb2a5ed7e64604e168cc98fe8f3a9e/packages/core/src/fields/types/text/index.ts#L106

Because a default value may not be set for a MySQL field with type Text, and I don't want to make the field nullable it is impossible to have the option to make a text field with the following properties:
```typescript
db: {
nativeType: 'Text',
isNullable: false
}
```

Proposal:

Add a feature to KeystoneJS that allows for the creation of non-nullable MySQL TEXT fields without default values.

Contributor guide

Open the contributing guide

Research direction

Start in packages/core/src/fields/types/text/index.ts around the linked defaultValue expression and trace how it reaches MySQL migration generation. Done means a non-nullable MySQL TEXT field can be created without a default value while preserving the requested field configuration.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, typescript
Domain
databases
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.