Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql Ruby Rails-在列中使用创建的_生成表_Sql_Ruby On Rails_Ruby_Timestamp - Fatal编程技术网

Sql Ruby Rails-在列中使用创建的_生成表

Sql Ruby Rails-在列中使用创建的_生成表,sql,ruby-on-rails,ruby,timestamp,Sql,Ruby On Rails,Ruby,Timestamp,RubyonRails有t.timestamps方法,它创建了两列,created_at和updated_at。我怎样才能只制作创建的_at列 这很有效 class CreateLinesSources < ActiveRecord::Migration def change create_table :lines_sources do |t| t.timestamps null: false end end end class CreateLines

RubyonRails有t.timestamps方法,它创建了两列,created_at和updated_at。我怎样才能只制作创建的_at列

这很有效

class CreateLinesSources < ActiveRecord::Migration
  def change
    create_table :lines_sources do |t|
      t.timestamps null: false
    end
  end
end
class CreateLinesSources
这两个我都想工作,但都失败了

class CreateLinesSources < ActiveRecord::Migration
  def change
    create_table :lines_sources do |t|
      t.created_at null: false
    end
  end
end
class CreateLinesSources

class CreateLinesSources
就像其他专栏一样。由于列名的原因,Rails仍将负责magic更新。

您可以执行以下操作:

def change
  create_table :lines_sources do |t|
    t.datetime :created_at, null: false
  end
end

两次尝试都可以,但都包含语法错误

第一次尝试的问题是t。位于数据类型而不是列名上

class CreateLinesSources < ActiveRecord::Migration
  def change
    create_table :lines_sources do |t|
      t.datetime :created_at, null: false
    end
  end
end

您是否尝试过在一次迁移中添加时间戳,然后在以后的迁移中删除
updated\u
。即使你没有使用它,最糟糕的情况是它会提醒你一个不应该发生的更新。当
ActiveRecord
需要它的时候,为什么还要费心删除它呢?节省的空间可以忽略不计。这个答案在我看来要好得多。因为在
列中创建
updated\u并在不久后将其删除有点奇怪。另外+1用于为时间戳添加Rails行为。
def change
  create_table :lines_sources do |t|
    t.datetime :created_at, null: false
  end
end
class CreateLinesSources < ActiveRecord::Migration
  def change
    create_table :lines_sources do |t|
      t.datetime :created_at, null: false
    end
  end
end
class CreateLinesSources < ActiveRecord::Migration
  def change
    create_table :lines_sources do |t|
      t.timestamps null: false
    end
    remove_column :line_sources, :updated_at, :datetime
  end
end