对于活动记录模式动态表名 [英] dynamic table names for Active Record models

查看:224
本文介绍了对于活动记录模式动态表名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有趣的活动记录的问题,我不太清楚 最干净的解决方案。 ,我与整合原有数据库 有一个奇怪的皱纹在其模式中其中一个逻辑表已经 分割成若干个物理表。每个表都有相同的 结构,但包含了不同项目的数据。

I have an interesting Active Record problem and I'm not quite sure what the cleanest solution is. The legacy database that I am integrating with has a strange wrinkle in its schema where one logical table has been 'partitioned' into several physical tables. Each table has the same structure, but contains data about different items.

我不是很擅长清楚地解释这个(你可以告诉!)。让我尝试 并用一个具体的例子说明。比方说,我们有车,有 的一个或多个车轮。通常情况下,我们会再present与一个汽车表和一个 轮台像这样:

I'm not great at explaining this clearly (as you can tell!). Let me try and explain with a concrete example. Let's say we have a Car, which has one or more Wheels. Normally we'd represent that with a Car table and a Wheels table like so:

CREATE TABLE cars (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255),
  ;etc
)

CREATE TABLE wheels (
  `id` int(11) NOT NULL auto_increment,
  `car_id` int(11) NOT NULL,
  `color` varchar(255),
  ;etc
)

到目前为止,一切都很好。但随着partioning战略,是在我的遗产 数据库它看起来更像是:

So far, so good. But with the 'partioning' strategy that is in my legacy database it would look more like:

CREATE TABLE cars (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255),
  ;etc
)

CREATE TABLE car_to_wheel_table_map (
  `car_id` int(11) NOT NULL,
  `wheel_table` varchar(255)
)

CREATE TABLE wheels_for_fords (
  `id` int(11) NOT NULL auto_increment,
  `car_id` int(11) NOT NULL,
  `color` varchar(255)
)

CREATE TABLE wheels_for_buicks (
  `id` int(11) NOT NULL auto_increment,
  `car_id` int(11) NOT NULL,
  `color` varchar(255)
)

CREATE TABLE wheels_for_toyotas (
  `id` int(11) NOT NULL auto_increment,
  `car_id` int(11) NOT NULL,
  `color` varchar(255)
)

所以在这里我们有一组wheels_for_x表,以及 其中包含的映射从car_id到car_to_wheel_table_map表 具体wheels_for_x包含轮子用于特定车。如果我 想找到一组轮子的车我首先要找出哪些 通过car_to_wheel_table_map表来使用车轮表,再看看 向上在car_to_wheel_table_map指定在车轮表中的记录。

So here we have a set of wheels_for_x tables, and a car_to_wheel_table_map table which contains a mapping from car_id to the specific wheels_for_x which contains the wheels for a specific car. If I want to find the set of wheels for a car I first have to find out which wheels table to use via the car_to_wheel_table_map table, and then look up records in the wheel table specified in the car_to_wheel_table_map.

首先,有人可以赐教,以是否有一个标准名称 这种技术?

Firstly, can someone enlighten me as to if there is a standard name for this technique?

其次,没有任何人有我如何使这项工作在任何指针 活动记录在一个不错的清洁方式。我看到它,我可以有一个方式 车轮模型,其中表名可以为每个实例来定义,或者我可以 动态创建示范班在运行时用正确的表名 如在映射表中指定的

Secondly, does anyone have any pointers on how I can make this work in Active Record in a nice clean way. The way I see it I can either have a Wheel model where the table name can be defined per instance, or I can dynamically create Model classes at runtime with the correct table name as specified in the mapping table.

修改:请注意,更改模式更接近到什么AR想是不是一种选择。各种传统codebases依靠这种模式,不能实事求是地进行修改。

EDIT: Note that changing the schema to be closer to what AR wants is not an option. Various legacy codebases rely on this schema and cannot realistically be modified.

推荐答案

数据库表分区是pretty的普遍做法真的。我会感到惊讶,如果有人没有这样做过。如何ActsAsPartitionable?的http://revolutiononrails.blogspot.com/2007/04/plugin-release-actsaspartitionable.html

DB table partitioning is pretty common practice really. I'd be surprised if someone hasn't done this before. How about ActsAsPartitionable? http://revolutiononrails.blogspot.com/2007/04/plugin-release-actsaspartitionable.html

另一种可能性:你可以DBMS pretend的分区是一个大表?我认为MySQL支持这一点。

Another possibility: can your DBMS pretend that the partitions are one big table? I think MySQL supports this.

这篇关于对于活动记录模式动态表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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