Back to Sails

`.create()`

docs/reference/waterline/models/create.md

12.12.20003.6 KB
Original Source

.create()

Create a record in the database.

usage
await Something.create(initialValues);

or

  • var createdRecord = await Something.create(initialValues).fetch();

Usage

ArgumentTypeDetails
1initialValues((dictionary))The initial values for the new record. (Note that, if this model is in "schemaful" mode, then any extraneous keys will be silently omitted.)

Note: For performance reasons, as of Sails v1.0 / Waterline 0.13, the initialValues dictionary passed into this model method will be mutated in-place in most situations (whereas in Sails/Waterline v0.12, this was not necessarily the case).

Result
TypeDescription
((dictionary?))For improved performance, the created record is not provided as a result by default. But if you chain .fetch(), then the newly-created record will be sent back. (Be aware that this requires an extra database query in some adapters.)
Errors
NameTypeWhen?
UsageError((Error))Thrown if something invalid was passed in.
AdapterError((Error))Thrown if something went wrong in the database adapter. See Concepts > Models and ORM > Errors for an example of how to negotiate a uniqueness error (i.e. from attempting to create a record with a duplicate that would violate a uniqueness constraint).
Error((Error))Thrown if anything else unexpected happens.

See Concepts > Models and ORM > Errors for examples of negotiating errors in Sails and Waterline.

Meta keys
KeyTypeDetails
fetch((boolean))If set to true, then the created record will be sent back.

Defaults to false.

For more information on meta keys, see .meta().

Example

To create a user named Finn in the database:

javascript
await User.create({name:'Finn'});

return res.ok();
Fetching the newly-created record
javascript
var createdUser = await User.create({name:'Finn'}).fetch();

sails.log('Finn\'s id is:', createdUser.id);

Negotiating errors

It's important to always handle errors from model methods. But sometimes, you need to look at errors in a more granular way. To learn more about the kinds of errors Waterline returns, and for examples of how to handle them, see Concepts > Models and ORM > Errors.

Notes

<docmeta name="displayName" value=".create()"> <docmeta name="pageType" value="method">