Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DuplicatePrimaryKeyError on non primary key field #628

Open
nodesocket opened this issue Aug 18, 2017 · 4 comments
Open

DuplicatePrimaryKeyError on non primary key field #628

nodesocket opened this issue Aug 18, 2017 · 4 comments

Comments

@nodesocket
Copy link

nodesocket commented Aug 18, 2017

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
Copy link
Owner

neumino commented Aug 18, 2017 via email

@nodesocket
Copy link
Author

nodesocket commented Aug 18, 2017

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
Copy link
Owner

neumino commented Aug 20, 2017

How do you save your document?

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

@nodesocket
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants