【问题标题】:Custom primaryKey Laravel + Apiato自定义主键 Laravel + Apiato
【发布时间】:2018-05-31 20:15:21
【问题描述】:

我使用自定义主键创建了表。它是 uuid "message_id"。

但是在尝试向其中插入数据时,我收到以下消息_

“'字段列表'中的未知列'id' ...”_。

试过

protected $primaryKey = 'message_id';
public $incrementing = false;

但没有运气。

我的模型看起来像:

protected $table = 'messages';

protected $primaryKey = 'message_id';
public $incrementing = false;

protected $fillable = [
    'message_id',
    'sender_id',
    'recipient_id',
    'message_text',
    'time',
];

查询:

$newMessage = $this->messageRepository->create([
    'sender_id'    => $sender_id,
    'recipient_id' => $recipient_id,
    'message_text' => $message_text,
]);

$newMessage->save();

【问题讨论】:

  • 显示表格描述。 REAL 表描述。
  • Schema::create('messages', function (Blueprint $table) { $table->uuid('message_id'); $table->uuid('sender_id'); $table-> uuid('recipient_id'); $table->string('message_text'); $table->boolean('seen'); $table->primary('message_id'); $table->timestamps(); $table ->softDeletes(); });
  • 不,打开一个 mysql shell,运行DESCRIBE yourtable; 并发布输出。你的代码看起来不错;我感觉您的表与迁移不同步。
  • create table messages ( message_id char(36) not null primary key, sender_id char(36) not null, recipient_id char(36) not null, message_text varchar(191) not null, seen tinyint(1) not null, created_at timestamp null, updated_at timestamp null, deleted_at timestamp null ) ;

标签: php laravel


【解决方案1】:

我发现了问题所在。在模型中我有Use Uuids;

trait Uuids 已经预定义了“'default_uuid_column' => 'id'

【讨论】:

    猜你喜欢
    • 2021-12-12
    • 1970-01-01
    • 2018-12-30
    • 2018-06-09
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多