I Have a Table Schema like:
Table users:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users, :id => false do |t|
t.int :pk, :limit => 8, primary: true,null: false
t.string :username
t.int :follower_count
t.timestamps null: false
end
add_index "users", ["pk"], name: "index_users_on_pk", unique: true, using: :btree
end
end
and Table stats:
class CreateStats < ActiveRecord::Migration
def change
create_table :stats,id: false do |t|
t.int :pk, :limit => 8, primary: true,null: false
t.int :state
t.timestamps null: false
end
add_index "stats", ["pk"], name: "index_stats_on_pk", unique: true, using: :btree
add_reference :stats, :users, index: true
end
end
column pk is the bigint column for table users and table stats.
the line add_reference :stats, :users, index: true adds new column user_id to table.
I want to make a one to one reference like stats.pk belongs to users.pk.
How to do that?
Related
I've applied uniqueness validation to "appeal_id" in model named "Campaigns". It asks me to add a unique index for uniqueness validation, I added campaign_id as a unique index. But it still shows the same error.
app/models/campaign.rb:9:3: C: Rails/UniqueValidationWithoutIndex: Uniqueness validation should have a unique index on the database column.
validates :appeal_id, uniqueness: { scope: :user_id }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
My schema for campaigns table looks like following:
create_table "campaigns", force: :cascade do |t|
t.string "title"
t.text "description"
t.bigint "appeal_id", null: false
t.bigint "user_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "campaign_id"
t.index ["appeal_id"], name: "index_campaigns_on_appeal_id"
t.index ["campaign_id"], name: "index_campaigns_on_campaign_id", unique: true
t.index ["user_id"], name: "index_campaigns_on_user_id"
end
"campaign.rb" file is as follows:
class Campaign < ApplicationRecord
has_many :donations, dependent: :destroy
belongs_to :appeal
belongs_to :user
validates :title, presence: true
validates :description, presence: true
validates :appeal_id, uniqueness: { scope: :user_id }
end
You're missing the compound index that you actually need to ensure that the combination of the two columns is unique.
Adding it is relatively simple:
class AddUniqueCompoundIndexToCampaigns < ActiveRecord::Migration[7.0]
def change
add_index [:appeal_id, :user_id], unique: true
end
end
I dropped my database and then edited my create_campaign migration and added this line of code
add_index :campaigns, [:appeal_id, :user_id], unique: true
in my migration. Then run the command
rails db:create db:migrate
which actually created my database again and resolved the issue of unique index.
class CreateCampaigns < ActiveRecord::Migration[7.0]
def change
create_table :campaigns do |t|
t.string :title
t.text :description
t.references :appeal, null: false, foreign_key: true
t.references :user, null: false, foreign_key: true
t.timestamps
end
add_index :campaigns, [:appeal_id, :user_id], unique: true
end
end
I created a table like below by ./bin/rails action_text:install and then, I can see the migration file like below.
However, I couldn't see what the long means for size.
# This migration comes from action_text (originally 20180528164100)
class CreateActionTextTables < ActiveRecord::Migration[6.0]
def change
create_table :action_text_rich_texts do |t|
t.string :name, null: false
t.text :body, size: :long
t.references :record, null: false, polymorphic: true, index: false
t.timestamps
t.index [ :record_type, :record_id, :name ], name: "index_action_text_rich_texts_uniqueness", unique: true
end
end
end
I have a Report model that have a step_id attribute , and I want to move this attribute to ReportSheets model, is there a way to generate the migration from terminal or should I write the code by my self :
class CreateReports < ActiveRecord::Migration[4.2]
def change
create_table :reports, id: :uuid do |t|
t.uuid :declarant_id
t.integer :reference
t.datetime :sent_at
t.uuid :step_id
t.boolean :is_archived, default: false
t.uuid :updated_by_id
t.string :device_id
t.timestamps null: false
end
add_index :reports, :reference, unique: true
add_index :reports, :declarant_id
add_index :reports, :device_id
add_index :reports, :step_id
end
end
class CreateReportSheets < ActiveRecord::Migration[4.2]
def change
create_table :report_sheets, id: :uuid do |t|
t.string :title, null: false
t.boolean :is_template, default: false
t.uuid :report_id
t.integer :seed_number
t.integer :order
t.hstore :steps, array: true, default: '{}', null: false
t.uuid :template_id
t.boolean :is_archived, default: false
t.uuid :updated_by_id
t.string :device_id
t.timestamps null: false
end
add_index :report_sheets, :report_id
add_index :report_sheets, :template_id
add_index :report_sheets, :seed_number
add_index :report_sheets, :is_template
add_index :report_sheets, :is_archived
add_index :report_sheets, :device_id
end
end
and in case of there is a relation between the 2 models like add_index :reports, :step_id ?
How can I create a relationship table in my migration class that both references are used in an unique index?
class CreateDiagnosticHypotheses < ActiveRecord::Migration
def change
create_table :diagnostic_hypotheses, :id => false do |t|
t.references :accident_indication, index: true
t.references :forms, index: true
t.timestamps null: false
end
add_foreign_key :diagnostic_hypotheses, :accident_indications
add_foreign_key :diagnostic_hypotheses, :forms, column: :diagnostic_hypothesis_id
end
end
When I run rake db:migrate it tries to create separate indexes. How can I create just one unique index with both :accident_indication and :forms references?
You can create unique composite index:
class CreateDiagnosticHypotheses < ActiveRecord::Migration
def change
create_table :diagnostic_hypotheses, :id => false do |t|
t.references :accident_indication
t.references :forms
t.timestamps null: false
end
add_index :diagnostic_hypotheses, [:accident_indication_id, :forms_id], :unique => true
add_foreign_key :diagnostic_hypotheses, :accident_indications
add_foreign_key :diagnostic_hypotheses, :forms, column: :diagnostic_hypothesis_id
end
end
I didn't try it myself, however, but I think you've got the idea.
I have generated three models (rails generate model): Professionals, ProfessionalCouncils and States. The Professionals table must have a reference to the States table, but the column should be named professional_council_state_id instead of state_id.
How can I achieve this in my migration code?
Here is how it is right now:
class CreateProfessionals < ActiveRecord::Migration
def change
create_table :professionals do |t|
t.string :name, limit: 70
t.string :number, limit: 15
t.references :professional_council, index: true
t.references :occupation_code, index: true
t.timestamps null: false
end
add_foreign_key :professionals, :professional_councils
add_foreign_key :professionals, :occupation_codes
end
end
class CreateStates < ActiveRecord::Migration
def change
create_table :states do |t|
t.string :description, limit: 40, null: false
t.string :abbreviation, limit: 2, null: false
t.integer :ibge_code
t.timestamps null: false
end
end
end