마이그레이션 생성-조인 테이블 생성
나는 많은 통해 보았다 SO
및 google
위해 테이블을 조인의 마이그레이션을 생성하는 게시물 has many and belongs to many
협회 아무것도 일을.
모든 솔루션이 빈 마이그레이션 파일을 생성합니다.
내가 사용하고 rails 3.2.13
난 두 테이블을 가지고 security_users
와 assignments
. 다음은 내가 시도한 몇 가지입니다.
rails generate migration assignments_security_users
rails generate migration create_assignments_security_users
rails generate migration create_assignments_security_users_join_table
rails g migration create_join_table :products, :categories (following the official documentation)
rails generate migration security_users_assignments security_user:belongs_to assignments:belongs_to
누구든지 두 테이블간에 조인 테이블 마이그레이션을 만드는 방법을 말할 수 있습니까?
이 명령을 실행하여 빈 마이그레이션 파일을 생성합니다 (자동으로 채워지지 않으므로 직접 채워야 함).
rails generate migration assignments_security_users
생성 된 마이그레이션 파일을 열고 다음 코드를 추가하십시오.
class AssignmentsSecurityUsers < ActiveRecord::Migration
def change
create_table :assignments_security_users, :id => false do |t|
t.integer :assignment_id
t.integer :security_user_id
end
end
end
그런 다음 rake db:migrate
터미널에서 실행 하십시오. 도움이 될 수있는 간단한 예를 통해 many_to_many 관계에 대한 퀴즈를 만들었습니다 .
명령 줄에서 create_join_table 명령을 자동으로 채우려면 다음과 같아야합니다.
rails g migration CreateJoinTableProductsSuppliers products suppliers
제품 모델 및 공급 업체 모델의 경우. Rails는 "products_suppliers"라는 제목의 테이블을 생성합니다. 복수형에 유의하십시오.
( generation
명령은 그냥 단축 할 수 있습니다. g
)
나는 보통 조인 테이블을 만들 때 "모델"파일도 갖고 싶어합니다. 그러므로 나는한다.
rails g model AssignmentSecurityUser assignments_security:references user:references
나는 이것이 rails 5에 대한 업데이트 된 대답이라고 믿습니다.
create_table :join_table_name do |t|
t.references :table_name, foreign_key: true
t.references :other_table_name, foreign_key: true
end
참고 URL : https://stackoverflow.com/questions/17765249/generate-migration-create-join-table
'developer tip' 카테고리의 다른 글
ASP.NET의 서버 측에서 Recaptcha 2 (CAPTCHA reCAPTCHA 없음) 유효성 검사 (0) | 2020.11.19 |
---|---|
Chunk.entrypoints : Chunks.groupsIterable을 사용하고 대신 Entrypoint 인스턴스별로 필터링합니다. (0) | 2020.11.19 |
큰 데이터 프레임에 인덱스 (숫자 ID) 열 추가 (0) | 2020.11.19 |
iPhone은 개발을 지원할 수 없습니다 (0) | 2020.11.19 |
Gradle 오류 : ': app : processDebugGoogleServices'작업에 대한 실행 실패 (0) | 2020.11.19 |