提交 fd353220 编写于 作者: G Guilherme Mansur

Better error message for calling columns_hash

When a record does not have a table name, as in the case for a record
with `self.abstract_class = true` and no `self.table_name` set the error
message raises a cryptic:
"ActiveRecord::StatementInvalid: Could not find table ''" this patch now
raises a new `TableNotSpecified Error`

Fixes: #36274
Co-Authored-By: NEugene Kenny <elkenny@gmail.com>
上级 aae270de
* Loading the schema for a model that has no `table_name` raises a `TableNotSpecified` error.
*Guilherme Mansur*, *Eugene Kenny*
* PostgreSQL: Fix GROUP BY with ORDER BY virtual count attribute.
Fixes #36022.
......
......@@ -38,6 +38,10 @@ class SerializationTypeMismatch < ActiveRecordError
class AdapterNotSpecified < ActiveRecordError
end
# Raised when a model makes a query but it has not specified an associated table.
class TableNotSpecified < ActiveRecordError
end
# Raised when Active Record cannot find database adapter specified in
# +config/database.yml+ or programmatically.
class AdapterNotFound < ActiveRecordError
......
......@@ -482,6 +482,9 @@ def load_schema
end
def load_schema!
unless table_name
raise ActiveRecord::TableNotSpecified, "#{self} has no table configured. Set one with #{self}.table_name="
end
@columns_hash = connection.schema_cache.columns_hash(table_name).except(*ignored_columns)
@columns_hash.each do |name, column|
define_attribute(
......
......@@ -1415,6 +1415,14 @@ def test_default_values_are_deeply_dupped
assert_not_includes SymbolIgnoredDeveloper.columns_hash.keys, "first_name"
end
test ".columns_hash raises an error if the record has an empty table name" do
expected_message = "FirstAbstractClass has no table configured. Set one with FirstAbstractClass.table_name="
exception = assert_raises(ActiveRecord::TableNotSpecified) do
FirstAbstractClass.columns_hash
end
assert_equal expected_message, exception.message
end
test "ignored columns have no attribute methods" do
assert_not_respond_to Developer.new, :first_name
assert_not_respond_to Developer.new, :first_name=
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册