不要怂,就是干,撸起袖子干!

Commit 59208d7c by Johannes Würbach

JSON: Cast key when using the equality operator

Fixed `operator does not exist` exception when using queries like:
```js
{
  'data.age': 4
}
```
1 parent b17f57ac
# Next # Next
- [FIXED] JSON cast key using the equality operator. [#3824](https://github.com/sequelize/sequelize/issues/3824)
- [FIXED] Map column names with `.field` in scopes with includes. [#4210](https://github.com/sequelize/sequelize/issues/4210) - [FIXED] Map column names with `.field` in scopes with includes. [#4210](https://github.com/sequelize/sequelize/issues/4210)
- [FIXED] `addScope` when the model does not have any initial scopes [#4243](https://github.com/sequelize/sequelize/issues/4243) - [FIXED] `addScope` when the model does not have any initial scopes [#4243](https://github.com/sequelize/sequelize/issues/4243)
- [FIXED] Fixed destroy with limit in PG when the primary key is aliassed [#4027](https://github.com/sequelize/sequelize/pull/4027) - [FIXED] Fixed destroy with limit in PG when the primary key is aliassed [#4027](https://github.com/sequelize/sequelize/pull/4027)
......
...@@ -1959,7 +1959,8 @@ var QueryGenerator = { ...@@ -1959,7 +1959,8 @@ var QueryGenerator = {
, $key , $key
, $cast , $cast
, $baseKey , $baseKey
, $tmp; , $tmp
, castKey;
if (path[path.length - 1].indexOf('::') > -1) { if (path[path.length - 1].indexOf('::') > -1) {
$tmp = path[path.length - 1].split('::'); $tmp = path[path.length - 1].split('::');
...@@ -1979,25 +1980,31 @@ var QueryGenerator = { ...@@ -1979,25 +1980,31 @@ var QueryGenerator = {
$baseKey = '('+$baseKey+')'; $baseKey = '('+$baseKey+')';
castKey = function ($item) {
var key = $baseKey;
if (!$cast) {
if (typeof $item === 'number') {
$cast = 'double precision';
}
if ($item instanceof Date) {
$cast = 'timestamptz';
}
}
if ($cast) {
key += '::'+$cast;
}
return key;
};
if (_.isPlainObject(item)) { if (_.isPlainObject(item)) {
_.forOwn(item, function ($item, $prop) { _.forOwn(item, function ($item, $prop) {
if ($prop.indexOf('$') === 0) { if ($prop.indexOf('$') === 0) {
$where[$prop] = $item; $where[$prop] = $item;
$key = $baseKey; $key = castKey($item);
if (!$cast) {
if (typeof $item === 'number') {
$cast = 'double precision';
}
if ($item instanceof Date) {
$cast = 'timestamptz';
}
}
if ($cast) {
$key += '::'+$cast;
}
$items.push(self.whereItemQuery(new Utils.literal($key), $where/*, _.pick(options, 'prefix')*/)); $items.push(self.whereItemQuery(new Utils.literal($key), $where/*, _.pick(options, 'prefix')*/));
} else { } else {
...@@ -2006,9 +2013,9 @@ var QueryGenerator = { ...@@ -2006,9 +2013,9 @@ var QueryGenerator = {
}); });
} else { } else {
$where.$eq = item; $where.$eq = item;
$key = castKey(item);
$key = new Utils.literal($baseKey); $items.push(self.whereItemQuery(new Utils.literal($key), $where/*, _.pick(options, 'prefix')*/));
$items.push(self.whereItemQuery($key, $where/*, _.pick(options, 'prefix')*/));
} }
}; };
......
...@@ -577,6 +577,18 @@ suite(Support.getTestDialectTeaser('SQL'), function() { ...@@ -577,6 +577,18 @@ suite(Support.getTestDialectTeaser('SQL'), function() {
default: "([data]#>>'{nested, attribute}') = 'value'" default: "([data]#>>'{nested, attribute}') = 'value'"
}); });
testsql('data.nested.attribute', 4, {
model: {
rawAttributes: {
data: {
type: new DataTypes.JSONB()
}
}
}
}, {
default: "([data]#>>'{nested, attribute}')::double precision = 4"
});
testsql('data.nested.attribute', { testsql('data.nested.attribute', {
$in: [3, 7] $in: [3, 7]
}, { }, {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!