Skip to content
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

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

Closed
2 of 7 tasks
erfanasbari opened this issue Mar 2, 2021 · 3 comments · Fixed by #14505
Closed
2 of 7 tasks

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

erfanasbari opened this issue Mar 2, 2021 · 3 comments · Fixed by #14505
Labels
dialect: mysql For issues and PRs. Things that involve MySQL (and do not involve all dialects). type: feature For issues and PRs. For new features. Never breaking changes.

Comments

@erfanasbari
Copy link

erfanasbari commented Mar 2, 2021

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.
@github-actions
Copy link
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. 🙂

@nishantc7
Copy link

Any Updates?

@ephys
Copy link
Member

ephys commented Feb 22, 2022

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

@ephys ephys reopened this Feb 22, 2022
@ephys ephys added dialect: mysql For issues and PRs. Things that involve MySQL (and do not involve all dialects). type: feature For issues and PRs. For new features. Never breaking changes. and removed stale labels Feb 22, 2022
@ephys ephys changed the title defaultValue for JSON DataType doesn't work. defaultValue for JSON DataType doesn't work in MySQL in MySQL 8. Feb 22, 2022
@ephys ephys mentioned this issue Oct 6, 2022
42 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dialect: mysql For issues and PRs. Things that involve MySQL (and do not involve all dialects). type: feature For issues and PRs. For new features. Never breaking changes.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants