-
-
Notifications
You must be signed in to change notification settings - Fork 281
Closed
Labels
bugSomething isn't workingSomething isn't working
Description
Basic checks
- I searched existing issues - this hasn't been reported
- I can reproduce this consistently
- This is a RubyLLM bug, not my application code
What's broken?
The rails generate ruby_llm:install
generator creates migrations with foreign key references to tables that haven't been created yet, causing migration failures.
How to reproduce
- Create a new rails app
- bundle add ruby_llm
- bundle install
rails db:create db:migrate
- rails generate ruby_llm:install
- rails db:migrate
Expected behavior
Migrations should run successfully, creating all necessary tables and relationships.
What actually happened
The generated migrations contain t.references
calls that reference tables (like models) that are created in later migrations. The migration attempts to create foreign key constraints to tables that don't exist yet.
❯ rails db:migrate
== 20250912205042 CreateChats: migrating ======================================
-- create_table(:chats)
bin/rails aborted!
StandardError: An error has occurred, this and all later migrations canceled: (StandardError)
PG::UndefinedTable: ERROR: relation "models" does not exist
/home/user/code/rubyllm-bug-repro/db/migrate/20250912205042_create_chats.rb:3:in 'CreateChats#change'
Caused by:
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "models" does not exist (ActiveRecord::StatementInvalid)
/home/user/code/rubyllm-bug-repro/db/migrate/20250912205042_create_chats.rb:3:in 'CreateChats#change'
Caused by:
PG::UndefinedTable: ERROR: relation "models" does not exist (PG::UndefinedTable)
/home/user/code/rubyllm-bug-repro/db/migrate/20250912205042_create_chats.rb:3:in 'CreateChats#change'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Workaround:
- Remove the t.references lines from the initial table creation migrations
- Create a separate migration after all tables are created to add the foreign key references:
rails generate migration add_references_to_chats_tool_calls_and_messages
class AddReferencesToChatsToolCallsAndMessages < ActiveRecord::Migration[8.1]
def change
add_reference :chats, :model, foreign_key: true
add_reference :tool_calls, :message, null: false, foreign_key: true
add_reference :messages, :chat, null: false, foreign_key: true
add_reference :messages, :model, foreign_key: true
add_reference :messages, :tool_call, foreign_key: true
end
end
Environment
- Ruby version: ruby 3.4.5
- RubyLLM version: 1.7.1
- OS: Linux
- postgres (PostgreSQL) 17.6
gobijanerylopez and florianfelsing
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working