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

userError: The model Action defined a primary key of id that has an invalid type. Valid primary key types are number and string #4614

Open
ganeshkbhat opened this issue Dec 2, 2018 · 5 comments
Labels
orm Related to models, datastores, orm config, Waterline, sails-hook-orm, etc. repro please Could you reproduce this in a repository for us?

Comments

@ganeshkbhat
Copy link

Waterline version: "waterline": "^0.13.5",
Node version: 10.7.0
NPM version: 6.1.0
Operating system: Ubuntu 18.x


I have read through the issue and have tried adding autoPK: true but seems like I am unable to use it. https://github.com/balderdashy/waterline/issues/301 It fails asking for Primary key to be defined.

Error when no Primary key is defined.

model = { identity: 'def', datastore: 'recMongo', schema: true, tableName: 'def', autoPK: true, attributes: {...} };

{ userError: Could not find a primary key attribute on the model Action. All models must contain an attribute that acts as the primary key and is guaranteed to be unique.

Error when Primary Key is defined.

model = { identity: 'def', datastore: 'recMongo', schema: true, tableName: 'def', autoPK: false, primaryKey: 'id', attributes: {"id": {
      "type": "string",
      "required": true,
      "index": true,
      "unique": true,
     "primaryKey": true
    }, ...} };

userError: The model 'Action' defined a primary key of 'id' that has an invalid type. Valid primary key types are 'number' and 'string'

Both way this fails. I am using sails-mongo as the adapter.

@sailsbot
Copy link

sailsbot commented Dec 2, 2018

@ganeshkbhat Thanks for posting, we'll take a look as soon as possible.


For help with questions about Sails, click here. If you’re interested in hiring @sailsbot and her minions in Austin, click here.

@ganeshkbhat
Copy link
Author

ganeshkbhat commented Dec 2, 2018

  • It works with the following config with schema is false or true (primaryKey is defined in extend configs and model attrs).
  • autoPK for sails-mongo is not being picked up as a valid option, I am forced to specify an primaryKey as _id and the create a model with attributes _id.
  • unique, autoIncrement, min,max,minLength,maxLength and primaryKey fields from within _id model definition is throwing errors saying that these fields are not recognised/valid.

Help requested, and any help is welcome to get autoPK:true working with schema:true. The goal here is to keep interchangeability with mysql/oracle:

const Waterline = require('waterline');
let waterline = new Waterline();
const datastores = {
    "recAuth": {
        "adapter": "mongodb",
        "url": "mongodb://localhost:27017/recAuth"
    }
};
const adapters = {
    'mongodb': require('sails-mongo')
};
waterline.registerModel(Waterline.Collection.extend({
    identity: 'def', 
    datastore: 'recAuth', 
    schema: false, 
    tableName: 'def', 
    autoPK: true, 
    primaryKey: '_id', 
    attributes: {
        "_id": {
            "type": "string"
        },
        "str": {
            "type": "string",
            "required": true
        }
    }
}));

function x(waterline) {
    let pr = new Promise(function (resolve, reject) {
        let model, modelFns;
        waterline.initialize({
            adapters: adapters,
            datastores: datastores
        }, (err, orm) => {
            if (err) reject(err);
            resolve(orm);
        });
    })
    return pr;
}

async function getOrm(waterline) {
    let orm = await x(waterline);
    return orm;
}

async function testMongo() {
  let sorm = await getOrm(waterline);
  let res = await sorm.collections.def.create({str: 'test'}).fetch();
  console.log(res);
  let result = await sorm.collections.def.find({});
  console.log(result);
}

testMongo();

@ganeshkbhat
Copy link
Author

I have raised a stackoverflow question to no help - https://stackoverflow.com/questions/53587393/sails-min-max-minlength-maxlength-for-number-and-string-types-gives-usererror Any help is welcome.

@rachaelshaw
Copy link
Member

@ganeshkbhat looks like you're using Waterline standalone, but if you have a chance would you try reproducing this in a new Sails app and link to the repo? (Might help us better understand what's going on)

@johnabrams7 johnabrams7 transferred this issue from balderdashy/waterline Mar 6, 2019
@johnabrams7 johnabrams7 added orm Related to models, datastores, orm config, Waterline, sails-hook-orm, etc. try this out please and removed needs cleanup labels Mar 6, 2019
@balderdashy balderdashy deleted a comment from sailsbot Mar 6, 2019
@johnabrams7
Copy link
Contributor

Hey @ganeshkbhat , we're moving all the Waterline issues to the main Sails repo (balderdashy/sails) for closer monitoring and greater exposure to the community. I see the last update on this one was reproducing the issue in a repo. We appreciate you taking the time to provide all you have so far, feel free to let us know how this one is going. Thanks.

@raqem raqem added the repro please Could you reproduce this in a repository for us? label Apr 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
orm Related to models, datastores, orm config, Waterline, sails-hook-orm, etc. repro please Could you reproduce this in a repository for us?
Development

No branches or pull requests

5 participants