【发布时间】:2015-12-14 20:07:38
【问题描述】:
我刚刚尝试将 2 个新表添加到我的架构、事件和日历中。在运行db:migrate 时,它运行没有错误,但只有events 表被拾取并放入可视化数据库。为什么会这样?它与 has_many / belongs_to 关联有关吗?很郁闷!
我刚刚运行了db:migrate:status,它显示:
Status Migration ID Migration Name
up 20151002160900 Create messages
up 20151123214812 Create calendars
up 20151123220903 Create customers
up 20151123221638 Create businesses
up 20151213150347 Create events
据说它正在创建日历,但它没有出现在我的数据库中。有谁知道如何解决这个问题?
这是我的数据库架构:
ActiveRecord::Schema.define(version: 20151213150347) do
create_table "businesses", force: true do |t|
t.string "name"
t.string "email"
t.string "normal_password_digest"
t.string "manager_password_digest"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "customers", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
t.integer "business_id"
t.string "first_name"
t.string "last_name"
t.string "email"
t.string "password_digest"
t.string "remember_digest"
t.string "role"
end
create_table "events", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
t.integer "calendar_id"
t.text "name"
t.datetime "start"
t.datetime "end"
t.string "location"
end
create_table "messages", force: true do |t|
t.text "content"
t.datetime "created_at"
t.datetime "updated_at"
end
end
这是我的 2 个迁移文件:
class CreateCalendars < ActiveRecord::Migration
def change
create_table :calendars do |t|
t.timestamps
t.references :customer, foreign_key: true
t.string :name
t.string :googlePass
t.string :googleID
end
end
end
class CreateEvents < ActiveRecord::Migration
def change
create_table :events do |t|
t.timestamps
t.references :calendar, foreign_key: true
t.text :name
t.datetime :start
t.datetime :end
t.string :location
end
end
end
我尝试在 create_calendars 上运行 db:migrate,它只是删除了所有内容,并没有创建表。
我尝试过运行db:drop 和db:setup。
我查看了我的事件表文本,虽然它包含“calendar_id”,但这不是外键,因为没有日历表(我假设)。
为什么会这样?
【问题讨论】:
-
我假设名为
:end的列会导致问题。您可以尝试更改为非保留的 Ruby 关键字并从头开始重试吗? -
那不是虽然不会创建的表
-
如何检查表是否已创建?运行
db:drop和db:migrate后会发生什么?也尝试运行rake db:migrate:down VERSION= 20151123214812和rake db:migrate:up VERSION= 20151123214812。并注意控制台中的输出,它应该打印一些东西。 -
一种可以帮助您避免使用像
end和begin这样的Ruby 关键字的好习惯是使用_at即starts_at和ends_at为日期和时间列添加后缀。您会注意到 ActiveRecord 对默认时间戳执行此操作。它读起来像英语:event.starts_at -
我刚试了一下,好像成功了!谢谢大家,我也把我的:start和:end改成了starts_at和ends_at
标签: ruby-on-rails ruby sqlite rubymine