由于检查table_exists,此代码在最后一行失败了吗?如何在Datamapper中正确执行此操作?
require 'sinatra'
require 'DataMapper'
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/blog.db")
class Post
include DataMapper::Resource
property :id, Serial
property :title, String
property :body, Text
property :created_at, DateTime
end
DataMapper.finalize
# automatically create the post table
DataMapper.auto_migrate! unless Post.table_exists?
Run Code Online (Sandbox Code Playgroud)
小智 5
贾斯汀,
如果您有dm-migrations必需(这基本上意味着您仍在使用RDBMS适配器),您可以执行以下操作以查明是否存在表(或该表中的列).
# Find out if the table named 'people' exists
DataMapper.repository(:default).adapter.storage_exists?('people')
# Find out if there's a 'name' column in the 'people' table
DataMapper.repository(:default).adapter.field_exists?('people', 'name')
Run Code Online (Sandbox Code Playgroud)
请注意,这些API方法只会混合到adapterif dm-migrations中,并且您使用的是DataObjectsAdapter后代.