balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.84k stars 1.95k forks source link

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 5 years ago

ganeshkbhat commented 5 years ago

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 modelAction. 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 commented 5 years ago

@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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

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.