Ambiguous Error
會遇到這個問題是因為在使用ransack時若關聯到其他表,就會自動left_outer_join該表,假設A表有B表的內容需要關聯,則A.join(“LEFT_OUTER_JOIN B on …”).select(…).where(…欲搜尋條件…)。假設A表跟B表有共同名字的欄位,假設叫status,若在where(…欲搜尋條件…)裡面沒有寫status是哪一個欄位,就會造成上述的錯誤。但大家可能會覺得這種錯誤不容易發生,但這次我遇到的狀況是有一段query寫在scope裡面,這段scope的某個撞名欄位並沒有指定是哪個資料表而造成的錯誤。
ActiveRecord::EagerLoadPolymorphicError
首先這一篇介紹lazy_loading, eager_loading的差別,那這個錯誤發生在當A要關聯B, C,且B, C間關係是多型的時候會發生的錯誤。在網路上找到一篇蠻詳細的文章。結論就是如果要用的話必須改為
A.join('LEFT OUTER JOIN B on ...')
.join('LEFT OUTER JOIN C on ...')
.where('...bla bla bla...')
PG::ConnectionBad
PG::ConnectionBad: could not connect to server: Connection refused
Is the server running on host “localhost” (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused
Is the server running on host “localhost” (127.0.0.1) and accepting
TCP/IP connections on port 5432?
如果遇到這一類問題的話,先檢查本地的資料庫是不是有問題。以這台電腦的狀況。除了在rails跳出錯誤訊息以外,我想要連本地的資料庫也看到一樣的錯誤:
> postgres -D /usr/local/var/postgres 2020–10–16 18:52:39.456 CST [50592] FATAL: database files are incompatible with server
2020–10–16 18:52:39.456 CST [50592] DETAIL: The data directory was initialized by PostgreSQL version 12, which is not compatible with this version 13.0.
這是因為我本地的postgresql被升級所導致的,只要先安裝postgres@12,並且關掉13版、開啟12版即可使用。
ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR
ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: update or delete on table “A” violates foreign key constraint “fk_rails_005b71ca83” on table “B”
如果我們在Model後面加上dependent: :destroy
把依賴的資料表一併刪除,並且執行該資料表的destroy回呼。但如果db層不給刪除的話,還是不能刪。上段錯誤訊息所代表的意思為A與B之間建立了關聯,所以不能刪除A。
ActiveRecord::StatementInvalid: PG::ConnectionBad
這是資料庫斷線所產生的錯,看到這個錯真的無解…
持續更新…