Note that there are some explanatory texts on larger screens.

plurals
  1. POForeign Key Issues in Rails
    primarykey
    data
    text
    <p>Took me a while to track down this error but I finally found out <em>why</em>. I am modeling a card game using the Rails framework. Currently my database looks (mostly) like this:</p> <pre><code>cards cards_games games ----- ----------- ----- id id id c_type card_id ... value game_id other_stuff </code></pre> <p>And the Rails ActiveRecord card.rb and game.rb currently look like this</p> <pre><code>#card.rb class Card &lt; ActiveRecord::Base has_and_belongs_to_many :player has_and_belongs_to_many :game has_and_belongs_to_many :cardsInPlay, :class_name =&gt; "Rule" end </code></pre> <hr> <pre><code>#game.rb class Game &lt; ActiveRecord::Base has_and_belongs_to_many :cards has_many :players has_one :rules, :class_name =&gt; Rule end </code></pre> <p>When I attempt to run a game and there are multiple games (more than 1), I get the error</p> <pre><code>ActiveRecord::StatementInvalid in GameController#start_game # example Mysql::Error: Duplicate entry '31' for key 1: INSERT INTO `cards_games` (`card_id`, `id`, `game_id`) VALUES (31, 31, 7) </code></pre> <p>Every time the action fails, cardid == id. This, I assume, has something with how Rails inserts the data into the database. Since there is no cardsgames object, I think it is just pulling card_id into id and inserting it into the database. This works fine until you have two games with the same card, which violates the primary key constraint on cardsgames. Being affluent with databases, my first solution to this problem was to try to force rails to follow a "real" definition of this relationship by dropping id and making cardid and gameid a primary key. It didn't work because the migration couldn't seem to handle having two primary keys (despite the Rails API saying that its okay to do it.. weird). Another solution for this is to omit the 'id' column in the INSERT INTO statement and let the database handle the auto increment. Unfortunately, I don't know how to do this either.</p> <p>So, is there another work-around for this? Is there some nifty Rails trick that I just don't know? Or is this sort of structure not possible in Rails? This is really frustrating because I <em>know</em> what is wrong and I <em>know</em> several ways to fix it but due to the constraints of the Rail framework, I just cannot do it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload