如何使用现有列轨定义迁移参考?

时间:2017-09-26 21:57:33

标签: ruby-on-rails activerecord

我想使用现有列定义模型迁移的外键,我想将codproducto设置为名为invmtoproducto的表的外键,这是我的新模型迁移:

public void AcceptCallback(IAsyncResult ar)
{
    try
    {
        // Get the socket that handles the client request.
        Socket listener = (Socket)ar.AsyncState;
        Socket handler = listener.EndAccept(ar);
        this.listener = handler;

        // Create the state object.
        StateObject state = new StateObject();

        handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
            new AsyncCallback(ReadCallback), state);
        mAcceptCallBackFlag = true;
    }
    catch (Exception ex)
    {
        //logging
    }

}

1 个答案:

答案 0 :(得分:0)

您可以使用

add_foreign_key :detalleinveacs, :invmtoproducto, column: :codproducto

您的迁移将如下所示:

class CreateDetalleinveacs < ActiveRecord::Migration[5.1]
  def change
    create_table :detalleinveacs do |t|
      t.integer :correlativo
      t.integer :cantidad
      t.decimal :valor, precision: 20, scale: 10
      t.decimal :costo, precision: 30, scale: 20
      t.string :nis
      t.datetime :feacceso
      t.integer :codproducto
      t.integer :idinveac
    end

    # Add a foreign key constraint 
    # from the 'detalleinveacs' table 
    # to the 'invmtoproducto' table 
    # where the foreign key column 'codproducto' of the 'detalleinveacs' table 
    # references the 'id' column of the 'invmtoproducto' table.
    add_foreign_key :detalleinveacs, :invmtoproducto, column: :codproducto
  end
end

如果外键引用了id表上不同于invmtoproducto的列,则可以覆盖该默认值:

add_foreign_key :detalleinveacs, 
                :invmtoproducto, 
                column: :codproducto,
                primary_key: :some_column_other_than_id

有关详细信息,请参阅documentation