0

I know that I can generate a model and set a constraint such as uniq by doing "rails generate model field1:string:uniq", for example. Is there a way I can set a "not null" constraint?

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
jeffdh5
  • 75
  • 2
  • 8

1 Answers1

0

I don't think you can do that while running the generator. Have you considered changing the migration file after your model is generated?

Migration files are inside db/migrations, and you could make a field as not nullable by adding a few parameters to your migration line, before running rake db:migrate.

Suppose you have a column named category_id:

t.integer :category_id, null: false, default: 0

This would create the field in the database in a not null format.

Marcelo Ribeiro
  • 1,718
  • 1
  • 13
  • 27