Nitul
Nitul

Reputation: 1035

waterline-model.create - primaryKey

I have below model with primary key id:

attributes: {
   id: {
      type: 'integer',
      autoIncrement: true,
      primaryKey: true,
      unique: true
   },
    name: {
      type: 'string',
      unique: true,
      required: true
   },
}

I am creating model as below:

var model = {
     id: undefined,
     name: 'name',
};

waterlinemodel.create(model).exec(function(error, result) {});

But it throws below error: Error (E_UNKNOWN) Encountered an unexpected error] Details: error: null value in column "id" violates not-null constraint

As, 'id' is a primary key, waterline should not look at what is the value of 'id' property.

How to resolve this error? I do not want to remove 'id' because I have created value object for the model and it contains all the attributes of model.I am setting value object property as I need. I do not need to set id property for creation.

Upvotes: 4

Views: 2114

Answers (3)

Jon Musselwhite
Jon Musselwhite

Reputation: 1811

2019 Answer

jaumard's link to documentation is coming up with a 404 error now, but I think things may have changed since 2015...

Sails.js has base attributes defined in config/models.js which looks something this in a freshly-generated project:

attributes: {
  createdAt: { type: 'number', autoCreatedAt: true, },
  updatedAt: { type: 'number', autoUpdatedAt: true, },
  id: { type: 'number', autoIncrement: true, },
}

Separately, the default primaryKey is set to id. If you wanted to override that, you would need to explicitly specify your new primaryKey in your full model definition. For example, if you wanted to make name your primaryKey you would use something like this:

module.exports = {
  primaryKey: 'name',
  attributes: {
    name: {
      type: 'string',
      unique: true,
      required: true
    },
    // ...
  },
}

Notice I put primaryKey outside attributes. This is important. Your primaryKey will also need the unique and required constraints.

Further, if you want to disable the id column so it is not committed to your database, you must replace id with the value false -- but then you must define a different primaryKey otherwise you will get an error when you start your application. The error shown in the question may be directly related to the fact that the model defined id explicitly as undefined. An example of how to disable id would look something like this:

module.exports = {
  primaryKey: 'name',
  attributes: {
    id: false,
    name: {
      type: 'string',
      unique: true,
      required: true
    },
    // ...
  },
}

Upvotes: 0

orthodoc
orthodoc

Reputation: 31

I am having exactly the same problem especially with the model configured to use postgresql. With it set to disk or memory, the resource is created but with postgresql the resource is not created with the not null constraint error.

The id is not being set irrespective of whether I set autoPK: true or not. Even setting the id attribute on the model with autoPK:false doesn't work.

Upvotes: 1

jaumard
jaumard

Reputation: 8292

As the documentation says :

Will set the primary key of the record. This should be used when autoPK is set to false.

attributes: {
  uuid: {
    type: 'string',
    primaryKey: true,
    required: true
  }
}

You need to set autoPK : false on your model.

The link to the doc : https://github.com/balderdashy/waterline-docs/blob/master/models.md#primarykey

Upvotes: 0

Related Questions