Open nodesocket opened 7 years ago
What's the code?
On Thu, Aug 17, 2017, 20:40 Justin Keller notifications@github.com wrote:
I have the following model:
'use strict';
const orm = require('../lib/orm.js'); const type = orm.type; const config = require('../lib/config.js'); const uuid = require('node-uuid');
const Check = module.exports = orm.createModel(orm.getTableName(__filename), { id: type.string().uuid(4).default(() => { return uuid.v4(); }), domain: type.string().required().allowNull(false).min(1), result: type.object().required().allowExtra(true).allowNull(true), created: type.date().default(orm.r.now()) }, { pk: 'id', table: config.rethinkdb.table, enforce_extra: 'strict' // jshint ignore:line });
Check.ensureIndex('domain'); Check.ensureIndex('created');
Which is the primary key is defined as id. However when I try to save() multiple documents with the same domain I am getting DuplicatePrimaryKeyError.
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/neumino/thinky/issues/628, or mute the thread https://github.com/notifications/unsubscribe-auth/ABZOu6o4OMB-t_jkHw7t4jzlT8rMgfHOks5sZQejgaJpZM4O7EH5 .
Sure, so I have two models:
// Domain.js
'use strict';
const orm = require('../lib/orm.js');
const type = orm.type;
const config = require('../lib/config.js');
const Domain = module.exports = orm.createModel(orm.getTableName(__filename), {
domain: type.string().required().allowNull(false).min(1),
created: type.date().default(orm.r.now())
}, {
pk: 'domain',
table: config.rethinkdb.table,
enforce_extra: 'strict' // jshint ignore:line
});
Domain.ensureIndex('created');
And:
// Check.js
'use strict';
const orm = require('../lib/orm.js');
const type = orm.type;
const config = require('../lib/config.js');
const uuid = require('node-uuid');
const Check = module.exports = orm.createModel(orm.getTableName(__filename), {
id: type.string().uuid(4).default(() => {
return uuid.v4();
}),
domain: type.string().required().allowNull(false).min(1),
result: type.object().required().allowExtra(true).allowNull(true),
created: type.date().default(orm.r.now())
}, {
pk: 'id',
table: config.rethinkdb.table,
enforce_extra: 'strict' // jshint ignore:line
});
Check.ensureIndex('domain');
Check.ensureIndex('created');
Note that I have not defined the hasMany()
and belongsTo()
relationship. Could that be the problem? Seems like there should not be a dependency on domain
being unique for Check
.
How do you save your document?
The duplicate key error is thrown on the primary key, secondary indexes can handle duplicate key.
After creating an id
field in Domain.js
and switching the primary key to id
from domain
, this problem went away. However, it still might be a bug/issue.
I have the following model:
The primary key is defined as
id
. However when I try tosave()
multiple documents with the samedomain
I am gettingDuplicatePrimaryKeyError
.