我的数据库包含两个表,我用rails g migration.
一张表包含电视节目,其中有多列数据描述每个节目。其中一列称为:watchlist。我忘记将 :watchlist 的默认值设置为 false,因为每个新节目的监视列表值都应为 false。在我的前端,我希望能够将该值从 false 切换为 true(将程序添加到监视列表的行为会将值从 false 更改为 true)。
我的后端使用 Rails API,因此我查阅了文档并尝试使用以下代码更改_column_default:
rails change_column_default :program, :watchlist, from: nil, to: false
Run Code Online (Sandbox Code Playgroud)
但得到这个错误:
Don't know how to build task 'change_column_default' (See the list of available tasks with `rails --tasks`)
Run Code Online (Sandbox Code Playgroud)
我尝试过rails --tasks,但没有在生成的列表中看到我需要的内容。
通过搜索其他问题,我找到了这个,但如果不需要的话,我宁愿不在模型上执行此操作。
当前(不正确)的迁移。:watchlist应该t.boolean :watchlist, default: false
class CreatePrograms < ActiveRecord::Migration[6.0]
def change
create_table :programs do |t|
t.string :url
t.string :name
t.string :network
t.string :image …Run Code Online (Sandbox Code Playgroud)