12

I was looking at using Sails for an app that we are developing.

I'm using the sails-postgresql adapter which uses the waterline orm.

I have an existing database that I want to connect to.

If I create a model using generate something

and then in my model I have

attributes:{
    title:{type:'String'}
}

If I browse to localhost/something the orm deletes all the columns in the something table except title.

Is there a way to stop it from doing this? This app should not delete columns on this database.

Thanks!

Travis Webb
  • 14,688
  • 7
  • 55
  • 109
Senica Gonzalez
  • 7,996
  • 16
  • 66
  • 108

1 Answers1

26

I am the author of Sails-Postgresql. Sails has an ORM called Waterline that it uses for managing data. The default setting assumes that you would want to auto-migrate your database to match your model attributes. Because Postgresql is a SQL database the Sails-Postgresql adapter has a setting called syncable that defaults to true. This would be false in a NoSQL database like redis.

This is easy to turn off if you want to manage your database columns yourself. You can add migrate: safe to your model and it won't try and update your database schema when you start Sails.

module.exports = {
  adapter: 'postgresql',
  migrate: 'safe',
  attributes: {
    title: { type: 'string' }
  }
};

Sails doesn't have anything like migrations in Rails. It uses auto-migrations to attempt to remove this from your development process and then leaves updating your production schema to you.

particlebanana
  • 2,416
  • 21
  • 22
  • 8
    Is there a configuration to make this true for every model? – Alex Sep 01 '13 at 18:55
  • 4
    Could up update the documentation to reflect all of the various options? I am new to sails, and have been trying to figure out how to actually USE a model within a controller for the better part of the last hour. Thanks – Andrew Rhyne Oct 09 '13 at 02:32
  • Since there are no migrations like in Rails, what happens when you need to run a migration that requires data transformation? In Rails, the proper way would be to do this in a migration file, but how would do this in Waterline/Sails? – Strawberry Feb 19 '14 at 09:11
  • @Alex in Sails v0.10.x, you can use the `sails.config.model` config in `config/model.js` to set any property for all models (think of it as the definition of your "base model"). – mikermcneil Mar 04 '14 at 20:09
  • @Strawberry There is an implementation of Rails-like manual migrations for Sails here: https://github.com/BlueHotDog/sails-migrations – mikermcneil Mar 04 '14 at 20:11