Rails database system: `initialize': no such table: main.dinners (SQLite3::SQLException) error with one table column but not with the others columns

huangapple go评论60阅读模式
英文:

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处遇到了错误,我不知道为什么,对于lunchbrunch,我没有问题:

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 &lt; ApplicationRecord
  belongs_to :monday, class_name: &#39;Day&#39;
  belongs_to :tuesday, class_name: &#39;Day&#39;
  belongs_to :wednesday, class_name: &#39;Day&#39;
  belongs_to :thursday, class_name: &#39;Day&#39;
  belongs_to :friday, class_name: &#39;Day&#39;
  belongs_to :saturday, class_name: &#39;Day&#39;
  belongs_to :sunday, class_name: &#39;Day&#39;
end

app/models/day.rb

class Day &lt; ApplicationRecord
  belongs_to :lunch, class_name: &#39;Meal&#39;
  belongs_to :brunch, class_name: &#39;Meal&#39;
  belongs_to :dinner, class_name: &#39;Meal&#39;
end

app/models/meal.rb

class Meal &lt; 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: &#39;Meal 1&#39;, description: &#39;Desc 1&#39;)
v2 = Meal.create(title: &#39;Meal 2&#39;, description: &#39;Desc 2&#39;)
v3 = Meal.create(title: &#39;Meal 3&#39;, description: &#39;Desc 3&#39;)


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 &quot;days&quot; (&quot;lunch_id&quot;, &quot;brunch_id&quot;, &quot;dinner_id&quot;, &quot;created_at&quot;, &quot;updated_at&quot;) VALUES (?, ?, ?, ?, ?)  [[&quot;lunch_id&quot;, 6], [&quot;brunch_id&quot;, 4], [&quot;dinner_id&quot;, 5], [&quot;created_at&quot;, &quot;2023-07-03 02:21:40.793290&quot;], [&quot;updated_at&quot;, &quot;2023-07-03 02:21:40.793290&quot;]]
  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&#39;: 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&#39;: 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? =&gt; 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 &lt;meal&gt;_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: &#39;meals&#39; }
  t.belongs_to :brunch, references: :meals, foreign_key: { to_table: &#39;meals&#39; }
  t.belongs_to :dinner, references: :meals, foreign_key: { to_table: &#39;meals&#39; }
  t.timestamps
end

huangapple
  • 本文由 发表于 2023年7月3日 10:44:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601576.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定