迁移后未创建 Sinatra 数据库 table
Sinatra db table not created after migration
我正在尝试使用 Sinatra 和 Sinatra/active 记录创建活动记录,我的迁移正在迁移到 Postgres 数据库,但没有在数据库中创建表,我在堆栈中检查了所有可能的解决方案溢出但没有用。我什至尝试从 db/migrate 文件夹中删除我的迁移文件,但输出仍然相同。一定是什么错误
Gemfile
source 'https://rubygems.org'
gem "sinatra"
gem "pg" #for postgres
gem "activerecord"
gem "sinatra-activerecord"
config.ru
require "./app"
run Sinatra::Application
rakefile.rb
require "./app"
require "sinatra/activerecord/rake"
app.rb
require 'sinatra'
require 'sinatra/activerecord'
db = URI.parse('postgres://project1:project1@localhost/*****')
ActiveRecord::Base.establish_connection(
:adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
:host => db.host,
:username => db.user,
:password => db.password,
:database => db.path[1..-1],
:encoding => 'utf8'
)
class Note < ActiveRecord::Base
end
class CreateNotes < ActiveRecord::Migration
def up
create_table :notes do |t|
t.string :title
t.text :body
t.timestamps
end
end
def down
drop_table :notes
end
end
迁移输出
user@user-Inspiron-5520:~/rails-apps/project1$ rake db:migrate
== 20150704053019 CreateNotes: migrating ======================================
== 20150704053019 CreateNotes: migrated (0.0000s) =============================
db 输出 (psql)
\dt
List of relations
Schema | Name | Type | Owner
--------+-------------------+-------+-----------
public | schema_migrations | table | project1
(1 row)
project1=# select * from schema_migrations;
version
----------------
20150704053019
(1 行)
注意: Project1 用户是拥有所有权限的超级用户
编辑
迁移文件20150704053019_create_notes.rb
class CreateNotes < ActiveRecord::Migration
def change
end
end
首先要注意@limekin 是第一个得到答案的评论:
The migration you are using to create notes table is actually kept in app.rb. But the migrate task looks for migrations inside db/migrate. If the migration file you gave at the bottom is inside the right directory, then move the migration definition from app.rb to there.
我将更详细地介绍一下。您用来创建 table 的函数属于迁移文件,因为迁移是创建、更改和删除 table、列和记录的原因。
因此,要解决您的问题,只需将应用文件中的函数上移到迁移文件中的更改函数即可。
我正在尝试使用 Sinatra 和 Sinatra/active 记录创建活动记录,我的迁移正在迁移到 Postgres 数据库,但没有在数据库中创建表,我在堆栈中检查了所有可能的解决方案溢出但没有用。我什至尝试从 db/migrate 文件夹中删除我的迁移文件,但输出仍然相同。一定是什么错误
Gemfile
source 'https://rubygems.org'
gem "sinatra"
gem "pg" #for postgres
gem "activerecord"
gem "sinatra-activerecord"
config.ru
require "./app"
run Sinatra::Application
rakefile.rb
require "./app"
require "sinatra/activerecord/rake"
app.rb
require 'sinatra'
require 'sinatra/activerecord'
db = URI.parse('postgres://project1:project1@localhost/*****')
ActiveRecord::Base.establish_connection(
:adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
:host => db.host,
:username => db.user,
:password => db.password,
:database => db.path[1..-1],
:encoding => 'utf8'
)
class Note < ActiveRecord::Base
end
class CreateNotes < ActiveRecord::Migration
def up
create_table :notes do |t|
t.string :title
t.text :body
t.timestamps
end
end
def down
drop_table :notes
end
end
迁移输出
user@user-Inspiron-5520:~/rails-apps/project1$ rake db:migrate
== 20150704053019 CreateNotes: migrating ======================================
== 20150704053019 CreateNotes: migrated (0.0000s) =============================
db 输出 (psql)
\dt
List of relations
Schema | Name | Type | Owner
--------+-------------------+-------+-----------
public | schema_migrations | table | project1
(1 row)
project1=# select * from schema_migrations;
version
----------------
20150704053019
(1 行)
注意: Project1 用户是拥有所有权限的超级用户
编辑
迁移文件20150704053019_create_notes.rb
class CreateNotes < ActiveRecord::Migration
def change
end
end
首先要注意@limekin 是第一个得到答案的评论:
The migration you are using to create notes table is actually kept in app.rb. But the migrate task looks for migrations inside db/migrate. If the migration file you gave at the bottom is inside the right directory, then move the migration definition from app.rb to there.
我将更详细地介绍一下。您用来创建 table 的函数属于迁移文件,因为迁移是创建、更改和删除 table、列和记录的原因。
因此,要解决您的问题,只需将应用文件中的函数上移到迁移文件中的更改函数即可。