Repository navigation
Closed
Labels
type: bugDEPRECATED: replace with the "bug" issue type
Description
What are you doing?
// Custom Type File
import Big from "big.js";
export default function sequelizeAdditions(Sequelize) {
const DataTypes = Sequelize.DataTypes;
/*
* Create new types
*/
class BIGJS extends DataTypes.DECIMAL {
public static parse(value) {
return new Big(value);
}
private _precision: number;
private _scale: number;
constructor(precision: number, scale: number) {
super();
this._precision = precision;
this._scale = scale;
}
public toSql() {
if (this._precision || this._scale) {
return `DECIMAL(${[this._precision, this._scale].filter(this.identity).join(",")})`;
}
return "DECIMAL";
}
// Optional, validator function
public validate(value, options) {
if (value instanceof Big) {
return true;
} else {
return false;
}
}
}
DataTypes.BIGJS = BIGJS;
// Mandatory, set key
DataTypes.BIGJS.prototype.key = DataTypes.BIGJS.key = "BIGJS";
// For convenience
// `classToInvokable` allows you to use the datatype without `new`
Sequelize.BIGJS = Sequelize.Utils.classToInvokable(DataTypes.BIGJS);
}
// Connection File
import { Sequelize } from "sequelize";
import BigJsCol from "./types/BigJsColumn";
BigJsCol(Sequelize);
const sequelize = new Sequelize(...);
// Model File
MyModel.init({
id: {
type: DataTypes.INTEGER.UNSIGNED,
autoIncrement: true,
primaryKey: true,
},
amount: new DataTypes.BIGJS(13, 4)
}, {
sequelize: Connection,
tableName: "MyModel",
});
// Simple Query
await MyModel.findAll();To Reproduce
Steps to reproduce the behavior:
- Defined models, and get everything wired up
- Run simple query
- Result for the field amount is not wrapped in a BigJs object
What do you expect to happen?
I would like the result coming back from the database to be wrapped
What is actually happening?
It is just returning a number :(
Environment
Dialect:
- mysql
- postgres
- sqlite
- mssql
- any
Dialect mysql2 version: 1.6.5
Database version: 5.7.26
Sequelize version: 5.9.3
Node Version: 12.x
OS: Mac OS 10.14.5
If TypeScript related: TypeScript version: 3.5.3
Tested with latest release: - No
- Yes, specify that version:
Reacted by Jacob Smith, Wes Roberts, Ignat Prokopovich and Yaroslav
Activity
papb commentedon Jul 24, 2019
Can you provide a MCVE? You included a lot of code but it doesn't seem self contained. I can't just copy-paste. Thanks!
papb commentedon Jul 24, 2019
Also, can you check if this problem happens in another dialect?
Bene-Graham commentedon Jul 29, 2019
I added a little sample project in this repo: https://github.com/Bene-Graham/SequelizeCustomColumnTypeBug
Couple of things I have noticed:
.sync()does not work with custom types it puts the wrong DB type in the create table statementEdit:
To switch between dialects just comment / uncomment the import in connectionToggle.ts
papb commentedon Jul 29, 2019
Was this intended? (I switched to sqlite)
Bene-Graham commentedon Jul 29, 2019
Yes, as far as I can tell the current set of types that ship with v5 don't have a way of not throwing a compile error.
papb commentedon Jul 29, 2019
Ignoring the compile error, I got
ReferenceError: connection is not definednow.[-]Custom Type not being consumed[/-][+]Custom datatype not working[/+]Bene-Graham commentedon Jul 29, 2019
interesting, can you give me the stack trace? I do not get that on my machine.
papb commentedon Jul 29, 2019
Nevermind, I messed up the typescript execution
I got:
Which I believe to be the intended reproduction of the bug 👍
Hopefully someone will be able to take a look soon. Good luck!!
Bene-Graham commentedon Jul 29, 2019
for that error you may need to uncomment:
in the index.ts file, I had that commented out due to it not working with the custom type(s) and mysql. In the end I just manually created the table and columns.
15 remaining items
awwit commentedon Nov 2, 2020
@papb hi, I think I know how to solve this problem. Try using the solution from here: https://stackoverflow.com/a/47268401/3684944
Mygod commentedon Nov 21, 2020
So could anyone please update the documentations to reflect the correct way to implement custom datatypes?
anthonyorona commentedon Feb 25, 2021
This is a snippet of code that shows how I was able to add the ltree extension as a custom datatype using the workaround of @mcuelenaere
`
brtdv commentedon Jun 9, 2021
+1
This also does not work for me, any updates here?
I followed all steps from the docs (https://sequelize.org/master/manual/extending-data-types.html - Example + Creating a MySQL specific data type), but when instantiating my Sequelize object, I get a
TypeError: Cannot read property 'mysql' of undefined.I'm using Sequelize 6.6.2.
The
src/models/index.js:10:18is the place where I start up Sequelize withconst instance = new Sequelize(dbCfg.dbname, dbCfg.user, dbCfg.password, { ...dbCfg });I did the mapping of a custom type like this:
This file is imported in
src/models/index.jslike this:Is there anyone that got custom datatypes to work? I've also tried searching code examples on Github in other libraries but this feature seems pretty obscure?
Mygod commentedon Jul 11, 2021
From my testing, it does not seem the
parsefunction is ever invoked?sequelize/lib/dialects/abstract/query-interface.js
Lines 1006 to 1021 in 1c1aa33
deviarchscs commentedon Oct 14, 2021
For those who still struggle with this, I worked around this issue by passing an Object implementing DataTypeAbstract AND toString + toSql.
Usage with sequelize-typescript:
I don't care about the options param in my case but you get it ...
github-actions commentedon Nov 9, 2021
This issue has been automatically marked as stale because it has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. If this is still an issue, just leave a comment or remove the "stale" label. 🙂
wasiq-naqi commentedon Apr 2, 2022
This works perfect for me, Thank you soo much
jedwards1211 commentedon Mar 20, 2024
@ephys is this only going to be fixed in v7?I had a custom data type working in v4, just tried upgrading to v6 and I'm also struggling, but really need to be able to upgrade to a non-beta version.Nevermind, I failed to notice the workaround above for the chaos
classToInvokablehath wrought. I will make a PR to the docs for v6.jedwards1211 commentedon Mar 20, 2024
As far as I can tell from reading the code and testing, it's not actually mandatory to subclass the custom data type for a dialect.
normalizeDataTypewill replace a data type with a dialect-specific one if it exists, but otherwise it will keep it as is, and use itstypes.postgresetc.It also seems unnecessary to monkeypatch new data types onto
Sequelize.DataTypes. I'm exporting my custom data type from one module and importing it directly into the module where I'm defining a model class and it works just fine. It's only necessary to monkeypatch ontoSequelize.DataTypes[dialect]if you want dialect-specific overrides to work.