1

Is the following kosher?

schema.rb
  users
    id:int (autoincr.)
    uid:string <----------------
    ...

  fb_friends
    id:int (autoincr.)
    uid:int <-------------------
    friend_uid:int
    ...

user.rb
   has_many :fb_friends, :primary_key => "uid", :foreign_key => "uid"

Note that the keys that tie a "user" and their "fb_friends" together are of different data types (string and int). In an ideal world, I probably wouldn't do this, but given the current system architecture, this seems to be the solution that would work best.

But is this okay to do / will it work without snafoos?

2 Answers 2

2

It depends on the DB engine you're using, e.g, for mysql =>

Corresponding columns in the foreign key and the referenced key must have similar internal data types inside InnoDB so that they can be compared without a type conversion. The size and sign of integer types must be the same. The length of string types need not be the same. For nonbinary (character) string columns, the character set and collation must be the same.

More info here, Anyways, I won't say it's a good practice to compare fields of different types :(.

Hope it helps you!

1

Not sure if it works, but I wouldn't recommend it if you have the option to change it. It's really easy to change a column type in a Rails migration:

change_column :users, :uuid, :integer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.