Postgresql - NUMERIC(3,2) not converted to a Number
See original GitHub issueIssue Description
What are you doing?
I have two models ( Exercise
and Exercise_Metrics
) which simplied definition could be :
module.exports = (sequelize, DataTypes) => {
let Exercise = sequelize.define('Exercise', {
title: {
type: DataTypes.STRING,
allowNull: false,
},
description: {
type: DataTypes.TEXT,
defaultValue: ""
},
file: {
type: DataTypes.STRING,
allowNull: true
},
state: {
type: DataTypes.ENUM("DRAFT", "PENDING", "VALIDATED", "NOT_VALIDATED"),
allowNull: false,
defaultValue: "DRAFT"
},
url: {
type: DataTypes.STRING,
allowNull: true
}
}, {
// https://sequelize.org/master/manual/models-definition.html#configuration
// Enable optimistic locking. When enabled, sequelize will add a version count attribute
// to the model and throw an OptimisticLockingError error when stale instances are saved.
// Set to true or a string with the attribute name you want to use to enable.
version: true,
// scopes : reuse more easily some common parts
// https://sequelize.org/master/manual/scopes.html
scopes: {
// include the metrics part
with_exercise_metrics() {
return {
include: [
// load exercise evaluation
{
model: sequelize.models.Exercise_Metrics,
as: "metrics",
required: true,
attributes: [
["vote_count", "votes"],
["avg_vote_score", "avg_score"]
]
}
]
}
},
}
});
Exercise.associate = function (models) {
// An exercise has one metrics
Exercise.hasOne(models.Exercise_Metrics, {
as: "metrics",
foreignKey: {
name: "exercise_id",
allowNull: false
}
});
};
return Exercise;
};
// This sub table is useful to save computation times for search
module.exports = (sequelize, DataTypes) => {
let Exercise_Metrics = sequelize.define('Exercise_Metrics', {
// to count how many votes for this exercise
vote_count: {
type: DataTypes.INTEGER,
defaultValue: 0,
allowNull: false
},
// the average score
avg_vote_score: {
type: DataTypes.DECIMAL(3,2),
defaultValue: 0.0,
allowNull: false
},
// the tags ids, pre-retrieved so that search is faster
tags_ids: {
type: DataTypes.ARRAY(DataTypes.INTEGER),
allowNull: false,
defaultValue: []
}
}, {
// https://sequelize.org/master/manual/models-definition.html#configuration
timestamps: false,
tableName: "Exercises_Metrics"
});
Exercise_Metrics.associate = function (models) {
// each exercise has only one Exercise Metrics
models.Exercise_Metrics.belongsTo(models.Exercise, {
as: "exercise",
foreignKey: {
name: "exercise_id",
allowNull: false
}
})
};
return Exercise_Metrics;
};
When using with_exercise_metrics
scope, I got this bug.
What do you expect to happen?
I expect “avg_score” to be a Number ( as it meets number validation rules )
"metrics": {
"votes": 42,
"avg_score": 3.5
}
What is actually happening?
"metrics": {
"votes": 42,
"avg_score": "3.50"
}
Additional context
Environment
- Sequelize version: 5.21.3
- Node.js version: 12.14.0
- Operating System: Windows 10
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): Postgres
- 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.
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (5 by maintainers)
Top Results From Across the Web
Documentation: 15: 8.1. Numeric Types
The types smallint , integer , and bigint store whole numbers, that is, numbers ... Inexact means that some values cannot be converted...
Read more >PostgreSQL: convert hex string of a very large number to a ...
My use case for this was converting hexadecimal SHA-1 hash values to integers. Without attention to numeric precision throughout ...
Read more >Convert the NUMBER data type from Oracle to PostgreSQL
A NUMERIC column may be defined with DATA_PRECISION higher than 18, but all column values fit well in the BIGINT or INT range...
Read more >How to Convert a String to a Numeric Value in PostgreSQL
Use the :: operator to convert strings containing numeric values to the DECIMAL data type. In our example, we converted the string '...
Read more >How to Convert a PostgreSQL Time Interval to a Numeric ...
Each unit is an integer. If it is not an integer, it needs to be converted into a lower-level integer. The integer is...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
If you could get another contributor to write the tests that go with it, that would be great: I’m busy creating a sscce for the order by issue.
No workaround except create a virtual getter ( which isn’t a solution at all as it causes problems when I use others alias ). I think you have to create a special case for that one here ; something like :