英文:
Rails database system: `initialize': no such table: main.dinners (SQLite3::SQLException) error with one table column but not with the others columns
问题
这是您提供的Rails 7应用程序代码的中文翻译:
我有一个简单的Rails 7应用程序,可以创建每周菜单。然后,用户可以选择他们想要的一周中的哪一天。
我在设计和使其工作方面遇到了一些问题。到目前为止,这是我所拥有的:
rails generate model WeeklyMenu monday:references tuesday:references wednesday:references thursday:references friday:references saturday:references sunday:references
rails generate model Day lunch:references brunch:references dinner:references
rails generate model Meal title:string description:text
app/models/weekly_menu.rb
class WeeklyMenu < ApplicationRecord
belongs_to :monday, class_name: 'Day'
belongs_to :tuesday, class_name: 'Day'
belongs_to :wednesday, class_name: 'Day'
belongs_to :thursday, class_name: 'Day'
belongs_to :friday, class_name: 'Day'
belongs_to :saturday, class_name: 'Day'
belongs_to :sunday, class_name: 'Day'
end
app/models/day.rb
class Day < ApplicationRecord
belongs_to :lunch, class_name: 'Meal'
belongs_to :brunch, class_name: 'Meal'
belongs_to :dinner, class_name: 'Meal'
end
app/models/meal.rb
class Meal < ApplicationRecord
end
这是我计划创建的方式,但是我在dinner
处遇到了错误,我不知道为什么,对于lunch
和brunch
,我没有问题:
v1 = Meal.create(title: '餐食 1', description: '描述 1')
v2 = Meal.create(title: '餐食 2', description: '描述 2')
v3 = Meal.create(title: '餐食 3', description: '描述 3')
day2 = Day.create(lunch: v1) # 没问题
day3 = Day.create(lunch: v1, brunch: v2) # 没问题
day1 = Day.create(lunch: v1, brunch: v2, dinner: v3) # 错误
day4 = Day.create(cena: v3, brunch: v2, lunch: v1) # 错误
WeeklyMenu.create(
monday: day1,
tuesday: day2,
wednesday: day1,
thursday: day2,
friday: day3
)
我遇到的错误:
Day Create (0.7ms) INSERT INTO "days" ("lunch_id", "brunch_id", "dinner_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?) [["lunch_id", 6], ["brunch_id", 4], ["dinner_id", 5], ["created_at", "2023-07-03 02:21:40.793290"], ["updated_at", "2023-07-03 02:21:40.793290"]]
TRANSACTION (0.1ms) rollback transaction
/Users/dev/.rvm/gems/ruby-3.2.0/gems/sqlite3-1.6.3-arm64-darwin/lib/sqlite3/database.rb:152:in `initialize': SQLite3::SQLException: no such table: main.dinners (ActiveRecord::StatementInvalid)
/Users/dev/.rvm/gems/ruby-3.2.0/gems/sqlite3-1.6.3-arm64-darwin/lib/sqlite3/database.rb:152:in `initialize': no such table: main.dinners (SQLite3::SQLException)
英文:
I have a simple rails 7 app where I can create a weekly menu. Then, users can select which day of the week they want.
I'm having a little trouble designing and make it in work. This is what I have so far:
rails generate model WeeklyMenu monday:references tuesday:references wednesday:references thursday:references friday:references saturday:references sunday:references
rails generate model Day lunch:references brunch:references dinner:references
rails generate model Meal title:string description:text
app/models/weekly_menu.rb
class WeeklyMenu < ApplicationRecord
belongs_to :monday, class_name: 'Day'
belongs_to :tuesday, class_name: 'Day'
belongs_to :wednesday, class_name: 'Day'
belongs_to :thursday, class_name: 'Day'
belongs_to :friday, class_name: 'Day'
belongs_to :saturday, class_name: 'Day'
belongs_to :sunday, class_name: 'Day'
end
app/models/day.rb
class Day < ApplicationRecord
belongs_to :lunch, class_name: 'Meal'
belongs_to :brunch, class_name: 'Meal'
belongs_to :dinner, class_name: 'Meal'
end
app/models/meal.rb
class Meal < ApplicationRecord
end
This is how I plan to create it but I'm getting and error with dinner
, I don't know why, with lunch and brunch I don't have a problem:
v1 = Meal.create(title: 'Meal 1', description: 'Desc 1')
v2 = Meal.create(title: 'Meal 2', description: 'Desc 2')
v3 = Meal.create(title: 'Meal 3', description: 'Desc 3')
day2 = Day.create(lunch: v1) # fine
day3 = Day.create(lunch: v1, brunch: v2) # fine
day1 = Day.create(lunch: v1, brunch: v2, dinner: v3) # error
day4 = Day.create(cena: v3, brunch: v2, lunch: v1) # error
WeeklyMenu.create(
monday: day1,
tuesday: day2,
wednesday: day1,
thursday: day2,
friday: day3
)
Error I'm getting:
Day Create (0.7ms) INSERT INTO "days" ("lunch_id", "brunch_id", "dinner_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?) [["lunch_id", 6], ["brunch_id", 4], ["dinner_id", 5], ["created_at", "2023-07-03 02:21:40.793290"], ["updated_at", "2023-07-03 02:21:40.793290"]]
TRANSACTION (0.1ms) rollback transaction
/Users/dev/.rvm/gems/ruby-3.2.0/gems/sqlite3-1.6.3-arm64-darwin/lib/sqlite3/database.rb:152:in `initialize': SQLite3::SQLException: no such table: main.dinners (ActiveRecord::StatementInvalid)
/Users/dev/.rvm/gems/ruby-3.2.0/gems/sqlite3-1.6.3-arm64-darwin/lib/sqlite3/database.rb:152:in `initialize': no such table: main.dinners (SQLite3::SQLException)
答案1
得分: 1
with lunch and brunch I don't have a problem
day2 = Day.create(lunch: v1) # fine
day3 = Day.create(lunch: v1, brunch: v2) # fine
That's not so fine because of Rails fail-open design. Take a look closer to day2 and day3: day2.valid2? => false
.
Then the final error message in your example: SQLite3::SQLException: no such table: main.dinners
. Rails tries to make a reference to dinners
table that doesn't exist. It happens because of how the migration is defined:
create_table :days do |t|
t.references :lunch, null: false, foreign_key: true
t.references :brunch, null: false, foreign_key: true
t.references :dinner, null: false, foreign_key: true
end
This schema won't work as expected. You can just use <meal>_id
integers instead as follows:
create_table :days do |t|
t.integer :lunch_id
t.integer :brunch_id
t.integer :dinner_id
end
With foreign keys:
create_table :days do |t|
t.belongs_to :lunch, references: :meals, foreign_key: { to_table: 'meals' }
t.belongs_to :brunch, references: :meals, foreign_key: { to_table: 'meals' }
t.belongs_to :dinner, references: :meals, foreign_key: { to_table: 'meals' }
t.timestamps
end
英文:
> with lunch and brunch I don't have a problem
day2 = Day.create(lunch: v1) # fine
day3 = Day.create(lunch: v1, brunch: v2) # fine
That's not so fine because of Rails fail-open design. Take a look closer to day2 and day3: day2.valid2? => false
.
Then the final error message in your example: SQLite3::SQLException: no such table: main.dinners
. Rails tries to make a reference to dinners
table that doesn't exist. It happens because of how the migration is defined:
create_table :days do |t|
t.references :lunch, null: false, foreign_key: true
t.references :brunch, null: false, foreign_key: true
t.references :dinner, null: false, foreign_key: true
end
This schema won't work as expected. You can just use <meal>_id
integers instead as follow:
create_table :days do |t|
t.integer :lunch_id
t.integer :brunch_id
t.integer :dinner_id
end
With foreign keys:
create_table :days do |t|
t.belongs_to :lunch, references: :meals, foreign_key: { to_table: 'meals' }
t.belongs_to :brunch, references: :meals, foreign_key: { to_table: 'meals' }
t.belongs_to :dinner, references: :meals, foreign_key: { to_table: 'meals' }
t.timestamps
end
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论