在轨多列主键 [英] multicolumn primary keys in rails

查看:118
本文介绍了在轨多列主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图迁移的桌面应用程序到轨(还面临着相当老式的现有数据库)。问题是,我没有一个唯一的ID在一列,但它是一个表中的三列的保证记录的唯一性。

I'm trying to migrate a desktop application to rails (also dealing with quite old fashioned existing database). The problem is that I don't have a unique ID in one column, but it's three columns of a table that guarantee uniqueness of a record.

由于我有三个表:

authors
  author_name,
  author_letter,
  author_nr1,
  author_nr2
  ...

titles
  titel_nr,
  titel_name,
  ...

author_titles
  titel_nr,
  author_letter,
  author_nr1,
  author_nr2

作者的主键由author_letter,author_nr1,author_nr2这里。

The "primary key" of authors consists of author_letter, author_nr1, author_nr2 here.

所以,我需要那种在这里一个多主键的有轨协会合作?还是我在这里走错方向?

So do I need sort of a multicolumn primary key here to have rails associations working? Or am I going in the wrong direction here?

推荐答案

没有。主键是(如导轨默认)记录的ID。

No. The Primary Key is (like rails default) the ID of the Record.

此外,您可以设置唯一键像

In addition you can set unique Keys like

    add_index :users, [:merchant_id, :email], unique: true
    add_index :users, [:merchant_id, :login], unique: true

这potects数据库。赶在Rails的独特性,你需要编写到模型:

This potects your database. To catch the uniqueness in Rails you need to write into your model:

  validates_uniqueness_of :email,    scope: :merchant_id
  validates_uniqueness_of :login,    scope: :merchant_id

这篇关于在轨多列主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆