- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用 mySql,我创建了 3 个表:vehicles
、cars
和 bikes
。表 cars
和 bikes
是表 vehicles
(子表)的特化。
vehicles
有一个名为 id
的主键。
汽车
和自行车
没有主键。相反,它们还有一个名为 id
的字段,其上有一个索引,其中包含与 vehicles.id
相同的值来充当外键。
使用 Laravel 5.3 框架,我尝试首先将新记录插入到 vehicles
中,然后将新记录插入 cars
中,如下所示:
$id = Vehicle::create(['name'=>'volvo'])->id;
Car::create(['id'=>$id, 'nbDoors'=>4]);
我收到以下错误:
QueryException in Connection.php line 770: SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value (SQL: insert into
cars
(nbDorrs
) values (3))
是否有解决方案或更好的数据库设计,以便我可以在两个表中插入数据并保持这种特化?
最佳答案
找到解决办法:我只是省略了将 id 添加到汽车模型中的可填充列!
class Car extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['id', 'nbDoors'];
关于php - Laravel 的表特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41525891/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 4 年前。
正如您在 this travis.yml 中看到的那样文件,我的代码依赖于一些第三方库,我在构建项目之前将它们安装在远程系统上。 Travis 每次推送提交时都会下载并构建这些库,这可以避免吗?我的意
我是一名优秀的程序员,十分优秀!