neumino / thinky

JavaScript ORM for RethinkDB
http://justonepixel.com/thinky/
Other
1.12k stars 128 forks source link

DuplicatePrimaryKeyError on non primary key field #628

Open nodesocket opened 7 years ago

nodesocket commented 7 years ago

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');

The primary key is defined as id. However when I try to save() multiple documents with the same domain I am getting DuplicatePrimaryKeyError.

neumino commented 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 .

nodesocket commented 7 years ago

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.

neumino commented 7 years ago

How do you save your document?

The duplicate key error is thrown on the primary key, secondary indexes can handle duplicate key.

nodesocket commented 7 years ago

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.