【发布时间】: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 ) ;