duanping3587 2014-06-09 21:29
浏览 102

非主键但自动递增ID为Eloquent ORM Laravel

So I've read the Laravel documentation on Eloquent and how it assumes that the table has a primaryKey called id.

I currently have a id field in the table that is auto-incrementing however, it is not a primary key. I have a composite primary key which eloquent doesn't fully support. This is not a huge issue for me. I don't mind working with the id field that is auto-incrementing.

I am just wondering if Eloquent will be able to work with id key even though it is not a primary key, but a auto incrementing one.

  • 写回答

1条回答 默认 最新

  • doulang9521 2014-06-09 21:57
    关注

    You may use any custom primary key in your table but only thing you need to explicitly declare the PK in the model, for example:

    protected $primaryKey = 'custom_pk';
    

    It'll work without any problem even when there is an id (non primary key) field in the table.

    评论

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法