gpt4 book ai didi

ruby-on-rails - ActionView::Template::Error (PG::UndefinedColumn: ERROR: 列 "weeknumber"不存在

转载 作者:行者123 更新时间:2023-11-29 11:40:43 24 4
gpt4 key购买 nike

我在 heroku 上使用 PG 数据库的 activeRecord 订单函数时遇到问题。我有以下代码,目前运行良好,除了记录偶尔会乱序。所以,我尝试在数据库上使用 .order 函数,现在我得到了 PG::UndefinedColumn: ERROR。如果我删除 .order 函数,它会再次运行。

当它崩溃时,模型或数据库中没有任何改变。它只是添加了 .order 函数来尝试控制数据库记录的排序。

此外,它在 sqlite3 数据库的开发中工作得很好。

这是有效的代码:

1 <table class="table table-striped table-bordered table-condensed table-hover">
2 <tr>
3 <td></td>
4 <% @pool.weeks.each do |week| %>
5 <td class="entry-header">
6 <%= "Week #{week.weekNumber}" %>
7 </td>
8 <% end %>
9 </tr>

这里是导致它崩溃的变化:

Change line 4 from this:
4 <% @pool.weeks.each do |week| %>
to this:
4 <% @pool.weeks.order("weekNumber ASC").each do |week| %>

一旦我添加 .order 函数,它就会在 heroku 上中断并出现以下错误:

app[web.1]: ActionView::Template::Error (PG::UndefinedColumn: ERROR:  column "weeknumber" does not exist
app[web.1]: 2: <tr>
app[web.1]: LINE 1: ...M "weeks" WHERE "weeks"."pool_id" = $1 ORDER BY weekNumber...
app[web.1]: : SELECT "weeks".* FROM "weeks" WHERE "weeks"."pool_id" = $1 ORDER BY weekNumber ASC):
app[web.1]:
app[web.1]: 1: <table class="table table-striped table-bordered table-condensed table-hover">
app[web.1]: 4: <% @pool.weeks.order("weekNumber ASC").each do |week| %>
app[web.1]: 3: <td></td>
app[web.1]: LINE 1: ...M "weeks" WHERE "weeks"."pool_id" = $1 ORDER BY weekNumber...
app[web.1]: 5: <td class="entry-header">
app[web.1]: app/views/pools/_show_board.html.erb:4:in `_app_views_pools__show_board_html_erb___2914673273129495301_70355170381800'
app[web.1]:
app[web.1]: 3: <td></td>
app[web.1]: app/views/pools/show.html.erb:28:in `_app_views_pools_show_html_erb__33868809623974782_70355169938300'
app[web.1]: 6: <%= "Week #{week.weekNumber}" %>
app[web.1]: ActionView::Template::Error (PG::UndefinedColumn: ERROR: column "weeknumber" does not exist
app[web.1]:
app[web.1]: 7: </td>
app[web.1]: : SELECT "weeks".* FROM "weeks" WHERE "weeks"."pool_id" = $1 ORDER BY weekNumber ASC):
app[web.1]: app/views/pools/_show_board.html.erb:4:in `_app_views_pools__show_board_html_erb___2914673273129495301_70355170381800'
app[web.1]: 2: <tr>
app[web.1]: 5: <td class="entry-header">
app[web.1]: app/views/pools/show.html.erb:28:in `_app_views_pools_show_html_erb__33868809623974782_70355169938300'
app[web.1]: 7: </td>
app[web.1]:
app[web.1]:
app[web.1]: ^
app[web.1]: 1: <table class="table table-striped table-bordered table-condensed table-hover">
app[web.1]: 4: <% @pool.weeks.order("weekNumber ASC").each do |week| %>
app[web.1]: 6: <%= "Week #{week.weekNumber}" %>
app[web.1]:
heroku[router]: at=info method=GET path=/pools/1 host=fb-pools.herokuapp.com fwd="76.250.116.65" dyno=web.1 connect=5ms service=142ms status=500 bytes=643
heroku[web.1]: Stopping all processes with SIGTERM

数据库中没有任何变化。唯一的变化是添加了 .order 函数。我猜我的迁移中缺少某些东西,但我不确定是什么。

这是与周模型关联的迁移文件:

class CreateWeeks < ActiveRecord::Migration
def change
create_table :weeks do |t|
t.integer :state
t.integer :pool_id

t.timestamps
end
end
end

class AddWeekNumberToWeeks < ActiveRecord::Migration
def change
add_column :weeks, :weekNumber, :integer
end
end

以下是 Pool 和 Week 模型:

class Pool < ActiveRecord::Base

POOL_TYPES = { PickEm: 0, PickEmSpread: 1, Survivor: 2, SUP: 3 }

has_many :users, through: :pool_memberships, dependent: :destroy
has_many :pool_memberships, dependent: :destroy
has_many :weeks, dependent: :destroy
has_many :entries, dependent: :destroy

# Make sure protected fields aren't updated after a week has been created on the pool
validate :checkUpdateFields, on: :update

attr_accessor :password

validates :name, presence: true,
length: { :maximum => 30 },
uniqueness: { :case_sensitive => false }
validates :poolType, inclusion: { in: 0..3 }
validates :allowMulti, inclusion: { in: [true, false] }
validates :isPublic, inclusion: { in: [true, false] }

...

end

class Week < ActiveRecord::Base

STATES = { Pend: 0, Open: 1, Closed: 2, Final: 3 }

belongs_to :pool
has_many :games, dependent: :destroy
has_many :picks, dependent: :destroy

accepts_nested_attributes_for :games

...

end

最佳答案

您的列名为 weekNumber,但 ActiveRecord 不区分大小写,因此正在寻找名为 weeknumber 的列。

将您的列名称更改为 week_number 并将您的订单更新为 week_number 就可以了。

关于ruby-on-rails - ActionView::Template::Error (PG::UndefinedColumn: ERROR: 列 "weeknumber"不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19165030/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com