使用复合键时导轨中的问题

时间:2018-02-21 08:14:58

标签: mysql ruby-on-rails activerecord foreign-keys composite-key

我在外部db mysql中使用复合键。我添加了composite_primary_keys gem,并且还将require'compone_primary_keys添加到我的environments.rb文件中。

当我尝试使用show动作时,我收到以下错误。

  

localhost:3000 / prereq_conf_expr_tbls / 1,PREQ 1,1

Mysql2::Error: You have an error in your SQL syntax; check the manual that 
corresponds to your MySQL server version for the right syntax to use near 
'1,1) LIMIT 1' at line 1: SELECT `PREREQ_CONF_EXPR_TBL`.* FROM 
`PREREQ_CONF_EXPR_TBL` WHERE (1,PREQ 1,1) LIMIT 1

使用这些命令为mysql创建关联表。

CREATE TABLE PREREQ_CONF_EXPR_TBL(CMD_ID INT, ENTRY_TYPE VARCHAR(255), 
FIELD_NO INT, EXPR_ID INT, LOGICAL_OP VARCHAR(255), PRIMARY KEY(CMD_ID, 
ENTRY_TYPE, FIELD_NO), FOREIGN KEY(EXPR_ID) REFERENCES EXPRESSION(EXPR_ID) ON 
DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY(CMD_ID) REFERENCES 
BASE_CMD_TBL(CMD_ID) ON DELETE CASCADE ON UPDATE CASCADE);

CONTROLLER

class PrereqConfExprTblsController < ApplicationController
    def index
        @prereqs = PREREQ_CONF_EXPR_TBL.all
    end

    def new
        @prereqs = PREREQ_CONF_EXPR_TBL.new
    end

    def show
        @prereqs = PREREQ_CONF_EXPR_TBL.find_by(params[:id])
    end

    def edit
        @prereqs = PREREQ_CONF_EXPR_TBL.find_by(params[:id])
    end

    def create
        @prereqs = PREREQ_CONF_EXPR_TBL.new(prereq_params)
        if @prereqs.save
            redirect_to @prereqs
        else
            render 'new'
        end
    end

    def update
        @prereqs = PREREQ_CONF_EXPR_TBL.find_by(params[:id])
        if @prereqs.update_attributes(prereq_params)
            redirect_to prereq_conf_expr_tbls_url
        else
            render 'edit'
        end
    end

    def destroy
        PREREQ_CONF_EXPR_TBL.find_by(params[:id]).destroy
        redirect_to prereq_conf_expr_tbls_url
    end

    private

        def prereq_params
            params.require(:prereq_conf_expr_tbl).permit(:CMD_ID, :ENTRY_TYPE ,:FIELD_NO ,:EXPR_ID, :LOGICAL_OP)
        end
end

MODEL

class PREREQ_CONF_EXPR_TBL < ExternalDbAccess
    self.table_name = "PREREQ_CONF_EXPR_TBL"
    self.primary_keys = 'CMD_ID', 'ENTRY_TYPE', 'FIELD_NO'
end

路线

resources :prereq_conf_expr_tbls

1 个答案:

答案 0 :(得分:0)

想出来。而不是使用.find_by使用.find

相关问题