Skip to content

defaultValue for JSON DataType doesn't work in MySQL in MySQL 8. #13065

Closed

Description

@erfanasbari

Issue Description

defaultValue for JSON DataType doesn't work.

What are you doing?

I am creating a model with a column called database and a defaultValue of a stringified JSON object.

Here is the link to the SSCCE for this issue: LINK-HERE

const server = sequelize.define("server", {
	id: {
		type: DataTypes.INTEGER,
		autoIncrement: true,
		primaryKey: true,
	},
	database: {
		type: DataTypes.JSONB,
		defaultValue: JSON.stringify({
			enabled: false,
			host: "localhost",
			user: "root",
			password: "",
			name: "dbname",
		}),
		allowNull: false,
	},
});
server.sync({ alter: true });

What do you expect to happen?

The query for syncing the database column must be this:

ALTER TABLE `servers` CHANGE `database` `database` JSON NOT NULL DEFAULT '{\"enabled\":false,\"host\":\"localhost\",\"user\":\"root\",\"password\":\"\",\"name\":\"cod4x\"}';

What is actually happening?

But the query for syncing the database column is this:

ALTER TABLE `servers` CHANGE `database` `database` JSON NOT NULL;

Additional context

This works well with JSONB datatype but I'm using MySQL and I need JSON.
EDIT: I think this PR is causing the problem.

Environment

  • Sequelize version: 6.5.0
  • Node.js version: 14.15.4
  • Operating System: WINDOWS 10 x64
  • If TypeScript related: TypeScript version: 4.1.5

Issue Template Checklist

How does this problem relate to dialects?

  • I think this problem happens regardless of the dialect.
  • I think this problem happens only for the following dialect(s):
  • I don't know, I was using PUT-YOUR-DIALECT-HERE, with connector library version XXX and database version XXX

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I don't know how to start, I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.

Activity

  1. github-actions commented on Oct 27, 2021

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

  2. nishantc7 commented on Feb 22, 2022

    @nishantc7

    Any Updates?

  3. ephys commented on Feb 22, 2022

    @ephys
    Member

    https://dev.mysql.com/doc/refman/8.0/en/json.html

    Prior to MySQL 8.0.13, a JSON column cannot have a non-NULL default value.

    Looks like we can enable default JSON values for mysql 8

  4. reopened this on Feb 22, 2022
  5. added
    dialect: mysql
    type: feature
    and removed on Feb 22, 2022
  6. changed the title [-]`defaultValue` for JSON DataType doesn't work.[/-] [+]`defaultValue` for JSON DataType doesn't work in MySQL in MySQL 8.[/+] on Feb 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    dialect: mysqltype: feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions