GithubHelp home page GithubHelp logo

twilightcoders / active_record-mti Goto Github PK

View Code? Open in Web Editor NEW
98.0 98.0 8.0 190 KB

ActiveRecord support for PostgreSQL's native inherited tables (multi-table inheritance)

License: MIT License

Ruby 100.00%
activerecord multi-table-inheritance postgresql

active_record-mti's People

Contributors

dredington avatar voltechs avatar yuki24 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

active_record-mti's Issues

Activerecord 5.1.5 ?

Bundler could not find compatible versions for gem "activerecord":
In snapshot (Gemfile.lock):
activerecord (= 5.1.5)

In Gemfile:
active_record-mti (~> 0.2.1) was resolved to 0.2.1, which depends on
activerecord (< 5, >= 4.1)

rails (= 5.1.5) was resolved to 5.1.5, which depends on
  activerecord (= 5.1.5)

Rails 6 Support

Hi @voltechs, I'm interested in using this in a Rails 6 project. Wondering if this is still maintained.

Thanks!

Suppor for Rails 5.2

Hi,
I just want to use active_record-mti with Rails 5.2 but it's not supported. The following errors appears when running bundle install:

Bundler could not find compatible versions for gem "pg":
  In snapshot (Gemfile.lock):
    pg (= 1.1.3)

  In Gemfile:
    pg

    active_record-mti (~> 0.3.2) was resolved to 0.3.2, which depends on
      pg (~> 0)

Running bundle update will rebuild your snapshot from scratch, using only
the gems in your Gemfile, which may resolve the conflict.

Should I make a project fork changing gemspec file with pg pointing to '~> 1'?

Feature Request: foreign key integrity

Postgresql does not allow for foreign key constraints from a base table to some other table. See Caveats section in https://www.postgresql.org/docs/9.5/static/ddl-inherit.html

Scenario:

  • You have a parent table called persons with a primary key id. You have students and teachers tables that inherit from persons.

  • You have another table called orders and it has an attribute person_id.

  • You'd like to add a foreign_key constraint from persons.id to orders.id. Such that anytime you inserted a student or teacher record it would maintain this constraint. However postgresql will not allow this as the record must exist in the base class.

Request:

  • Optionally allow a way to maintain this foreign_key constaint at the application layer (MTI)

  • For instance, if you tried to insert an orders record and the person did exist in the persons table then MTI would throw a foreign constraint exception.

Class not recognized as MTI class

Unfortunately my AdminUser class is not recognized as a MTI class. :(
I'm running Rails 4.2.10 and this is my setup:

ActiveRecord::Migration.create_table :users do |t|
  t.string :first_name, :last_name
  t.timestamps null: false
end

ActiveRecord::Migration.create_table :admin_users, inherits: :users do |t|
  t.text :roles, array: true, default: [], null: false
end
# db/structure.rb
CREATE TABLE users (
    id integer NOT NULL,
    first_name character varying,
    last_name character varying,
    created_at timestamp without time zone NOT NULL,
    updated_at timestamp without time zone NOT NULL,
);

CREATE TABLE admin_users (
    roles text[] DEFAULT '{}'::text[] NOT NULL
)
INHERITS (users);
# app/models/application_record.rb
class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
end

# app/models/user.rb
class User < ApplicationRecord
end

class AdminUser < User
end

When I check AdminUser.mti_type_column and AdminUser.tableoid_column on the console, they are both nil, which they shouldn't be according to the specs.
What can I do to further help debugging this problem?

inheriting indexes can get a > 64 character limit error

Issue:
When mti adds indexes to children tables it can fail if the generated index name is too long. Rails' add_index function generates the index name by default using the table name and the column your indexing (or array of columns if it's a compound index). If this constructed table name is > 64 characters then add_index will fail.

One thing to note is the :name attribute isn't being passed to add_index when constructing the child index in "active_record-mti/lib/active_record/mti/connection_adapters/postgresql/schema_statements.rb #create_table"

Steps to Reproduce:
Add the following to the spec/schema.rb file and try to run tests.

create_table :vehicles, force: true do |t|
    t.string :color
    t.string :type # Inheritance column
    t.timestamps null: false
  end

  add_index :vehicles, [:color, :type]

  create_table "vehicles/trucks", force: true, inherits: :vehicles do |t|
    t.integer :bed_size
  end

  # different schema of parent
  create_table "vehicles/some_really_really_really_really_long_table_name", force: true, inherits: :vehicles do |t|
    t.integer :long_size
  end

  # same schema of parent
  create_table "vehicles/some_other_really_really_really_long_table_name", force: true, inherits: :vehicles do |t|
end

Expected Result:
It creates the compound indexes on the children tables.

Actual Result:
The following error is thrown:
Index name 'index_vehicles/some_really_really_really_really_long_table_name_on_color_and_type' on table 'vehicles/some_really_really_really_really_long_table_name' is too long; the limit is 63 characters (ArgumentError)

Fixture loading issue

Hi guys,
I have an issue with test fixture. The fixture related to extended model is not loaded into the test DB.

My environment: OSX, Rails 5.1.4, Ruby 2.4.2, active_record-mti 0.3.0.pre.rc4

My code (extract)
Migrations:

create_table :rules do |t|
      t.string :regex,             null: false
     ...
      t.timestamps
    end

create_table :rule_products, inherits: :rules do |t|
      t.xxx xxx ....
end

Models:

class Rule < ApplicationRecord
  validates :regex, presence: true
  ...
end

class RuleProduct < Rule
  self.table_name = 'rule_products'
  ...
end

Fixtures:

--- rules.yml
one:
  regex: rule_one.*

two:
  regex: rule_two.*

--- rule_products.yml
one:
  regex: one.*
  otherfield: value1

two:
  regex: two.*
  otherfield: value2

Now in the test, if I use:

rule = rules(:one)

all works fine, but, if I use:

rule = rule_products(:one)

I got the following error:

Minitest::UnexpectedError: ActiveRecord::RecordNotFound: Couldn't find RuleProduct with 'id'=980190962

and, if I check the test DB, the related table is empty.

I'm doing something wrong?
Thanks for the help.
Best Regards,
Nicola

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.