I have an Entity where I am trying to store user roles, I believe simple-array is the best option for mysql.
@Column({ type: 'simple-array', select: false, default: 'User' })
roles: string[];
This results in:
[Nest] 179516 - 2018-7-9 17:44:32 [TypeOrmModule] Unable to connect to the database. Retrying (7)... +3001ms
QueryFailedError: ER_BLOB_CANT_HAVE_DEFAULT: BLOB, TEXT, GEOMETRY or JSON column 'roles' can't have a default value
at new QueryFailedError (C:\Users\Tyler\Documents\GitHub\tribot-stats\api\src\error\QueryFailedError.ts:7:9)
at Query._callback (C:\Users\Tyler\Documents\GitHub\tribot-stats\api\src\driver\mysql\MysqlQueryRunner.ts:157:37)
at Query.Sequence.end (C:\Users\Tyler\Documents\GitHub\tribot-stats\api\node_modules\mysql\lib\protocol\sequences\Sequence.js:88:24)
at Query.ErrorPacket (C:\Users\Tyler\Documents\GitHub\tribot-stats\api\node_modules\mysql\lib\protocol\sequences\Query.js:90:8)
at Protocol._parsePacket (C:\Users\Tyler\Documents\GitHub\tribot-stats\api\node_modules\mysql\lib\protocol\Protocol.js:279:23)
at Parser.write (C:\Users\Tyler\Documents\GitHub\tribot-stats\api\node_modules\mysql\lib\protocol\Parser.js:76:12)
at Protocol.write (C:\Users\Tyler\Documents\GitHub\tribot-stats\api\node_modules\mysql\lib\protocol\Protocol.js:39:16)
at Socket.<anonymous> (C:\Users\Tyler\Documents\GitHub\tribot-stats\api\node_modules\mysql\lib\Connection.js:103:28)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
I have searched similar issues for simple-json (tried this first) and saw the same issue.
simple-json maps to string in the database and default is sql's DEFAULT value, so you need to set a string, e.g. default: "{}"
Is it not possible to set a default value for simple-array or simple-json or am I doing something wrong?
Thanks
Also noticed the simple-array column-type for a string[] which is probably more appropriate.
I'm unable to set a default column type on this column either, tried in many different formats. Any clarification?
Default value in mysql simple-json @Column({default: ''})
Issue setting default value in mysql simple-array / simple-json @Column({default: ''})
Jul 9, 2018
await this.userRepository.insert({
email: createUserDto.email,
username: createUserDto.username,
password: hash,
roles: ['User'],
And inserting a user with roles: ['User'] in my user.service which is a completely fine workaround but unnecessary if I can control default role logic in the column itself.