anlek / mongify

Mongify allows you to map your data from a sql database and into a mongodb document database.
http://github.com/anlek/mongify
MIT License
317 stars 82 forks source link

How to reference to collection itself? #111

Closed dinhkhanh closed 8 years ago

dinhkhanh commented 8 years ago

I connect databases, translate and process successfully.

table "Category", :rename_to => 'categories' do
    column "CatID", :key
    column "ParentID", :rename_to => 'parent', :references => 'categories'
    column "CatName", :string, :rename_to => 'title'
    column "CatNameAlias", :string, :rename_to => 'slug'
    column "Active", :integer, :rename_to => 'is_active'
    column "CatMetaTitle", :text, :rename_to => 'meta_title'
    column "CatMetaDescription", :text, :rename_to => 'meta_description'
    column "CatMetaKeywords", :text, :ignore => true
end

Problem is parent field in MongoDB document keeps using value of ParentID in MySQL. It's expected to be an ObjectID references to a categories document.

How to correct it?

Thanks in advanced.

dinhkhanh commented 8 years ago

I figured out why it din't work.

Mongify takes ParentID value as string, that's why it cannot recognize the id to reference.

Just a slightly modify to make it work.

table "Category", :rename_to => 'categories' do
    column "CatID", :key
    column "SectionID", :integer, :ignore => true
    column "ParentID", :integer, :rename_to => 'parent', :references => :categories
    column "CatName", :string, :rename_to => 'title'
    column "CatNameAlias", :string, :rename_to => 'slug'
    column "Active", :integer, :rename_to => 'is_active'
    column "CatMetaTitle", :text, :rename_to => 'meta_title'
    column "CatMetaDescription", :text, :rename_to => 'meta_description'
    column "CatMetaKeywords", :text, :rename_to => 'meta_keywords'

    before_save do |row|
        row.parent = (row.parent).to_i
    end
end

(row.parent).to_i convert the value to integer. Now it can make the refernces correctly.