Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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
Ruby 我可以用DataMapper更改SQLite列名吗?_Ruby_Sqlite_Database Migration_Datamapper - Fatal编程技术网

Ruby 我可以用DataMapper更改SQLite列名吗?

Ruby 我可以用DataMapper更改SQLite列名吗?,ruby,sqlite,database-migration,datamapper,Ruby,Sqlite,Database Migration,Datamapper,DataMapper是个新手,我想知道是否可以使用DataMapper.auto_updgrade更改SQLite数据库中现有列的列名 如果我在song.rb中有以下内容 require 'date' require 'dm-core' require 'dm-migrations' DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/development.db") class Song include DataMapper::Resour

DataMapper是个新手,我想知道是否可以使用
DataMapper.auto_updgrade
更改SQLite数据库中现有列的列名

如果我在
song.rb中有以下内容

require 'date'
require 'dm-core'
require 'dm-migrations'

DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/development.db")

class Song
  include DataMapper::Resource
  property :id, Serial
  property :title, String
  property :music_by, String
  property :lryics_by, String
  property :lyrics, Text
  property :length, Integer
  property :released_on, Date

  def released_on=date
    super Date.strptime(date, '%m/%d/%Y')
  end

end

DataMapper.finalize
经过
Song.auto\u迁移

2.0.0-p598 :004 > Song.new
 => #<Song @id=nil @title=nil @music_by=nil @lyrics_by=nil @lyrics=nil @length=nil @released_on=nil> 

并更改数据库列名,但保留任何现有数据

我尝试了
Song.auto\u升级
并添加一个空的新列,保留原始列和数据。另一方面,我的歌曲。新对象看起来不错

2.0.0-p598 :004 > Song.new
 => #<Song @id=nil @title=nil @music_by=nil @words_by=nil @lyrics=nil @length=nil @released_on=nil> 
2.0.0-p598:004>Song.new
=> # 
似乎我需要一种迁移方式,就像ActiveRecord(我已经使用过一点ORM)处理迁移一样。或者我需要用SQL或应用程序或Firefox SQLlite插件更改列名

更新: 我现在想知道这是否更像是SQLite的东西而不是DataMapper的东西。当我在Firefox的SQLite管理器插件中删除一列时,我收到了以下消息:

这是一个潜在的危险操作。SQLite不支持可以更改表中列的语句。这里,我们试图通过查看pragma table_info来重构新的CREATESQL语句,该表不包含关于现有表结构的完整信息


是否仍要继续?

dm迁移可以做到这一点,但不能用于SQLite,这主要是因为SQLite不支持重命名列,因为它有一个有限的ALTER TABLE实现()

有一个重命名列迁移,但正如您从dm migrations TableModifier类()中看到的,它不适用于SQLite:

def rename_column(name, new_name, opts = {})
    # raise NotImplemented for SQLite3
    @statements << @adapter.rename_column_type_statement(table_name, name, new_name)
end
def rename_列(name,new_name,opts={})
#为SQLite3实现了raise NotImplemented

@语句在StackOverflow上找到此项,以便在SQLite中进行更改:
2.0.0-p598 :004 > Song.new
 => #<Song @id=nil @title=nil @music_by=nil @words_by=nil @lyrics=nil @length=nil @released_on=nil> 
def rename_column(name, new_name, opts = {})
    # raise NotImplemented for SQLite3
    @statements << @adapter.rename_column_type_statement(table_name, name, new_name)
end