- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用 Laravel 5.8.31 我似乎遇到了表中不应该为空的内容的完整性约束冲突。问题是它告诉我迁移文件中的 posts.title 不应该为空,但该变量甚至不在迁移表中。
SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint >failed: posts.title (SQL: insert into "posts" ("caption", "image", "user_id", >"updated_at", "created_at") values (kjhgj, C:\xampp\tmp\phpE3B7.tmp, 1, 2019->08-14 07:14:03, 2019-08-14 07:14:03))
我正在尝试学习 Laravel,并且一直在关注这个 YouTube 教程 https://www.youtube.com/watch?v=ImtZ5yENzgE
从浏览器获得的错误页面中,我可以看到错误是在 PostsController.php 中引发的。
我到达了 2:04:00,但遇到了这个问题。我读过这里提出和回答的许多其他问题,但没有运气。
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('caption');
$table->string('image');
$table->timestamps();
$table->index('user_id');
// THERE IS NO 'title' entry.
});
}
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
//Tutorial uses guarded rather than fillable
protected $guarded = [];
public function user(){
return $this->belongsTo(User::class);
}
}
class PostsController extends Controller
{
public function create()
{
return view('posts.create');
}
public function store()
{
$data = request()->validate([
'caption' => 'required',
'image' => ['required','image'],
]);
auth()->user()->posts()->create($data); //ERROR IS THROWN HERE
dd(request()->all());
}
}
我有一个页面,用户可以在其中输入标题并上传文件。无需任何验证,我就可以 dd(request()->all());
查看标题和文件是否已收到。但是,当我在 Controller 文件中添加验证和 auth() 行时,我发现“posts.title”违反了完整性约束
我希望将标题和图像添加到数据库中。
第一次发帖,因为我通常都能找到答案,但这次我卡住了。
编辑:这是错误: Error Image
@Vipertecpro 能够解决我的问题。我必须运行以下两个命令:php artisan migrate:refresh --seed
然后 php artisan optimize:clear
。
@Don'tPanic 在本文的评论中解释了这些命令为何有效。
最佳答案
要允许 Laravel 在数据库中进行大量插入或更新,您必须在模型中声明 $fillable
变量。
就您而言:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
//Tutorial uses guarded rather than fillable
//protected $guarded = []; // If you use $fillable do not use $guarded
// HERE
protected $fillable = ['user_id', 'caption', 'image'];
public function user(){
return $this->belongsTo(User::class);
}
}
如果你不这样做,Laravel 会尝试将数组的所有数据插入数据库表中。
关于php - 违反完整性约束 : 19 NOT NULL constraint failed for something not in migration table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57490585/
因此,我正在使用 rails(ruby 1.9.3p392、rails 3.2、sqlite3 db)并尝试将无处不在的博客教程代码部署到“生产”服务器(apache、passenger、ubuntu
我继承了一个项目,该项目设置为使用 Entity Framework 在新数据库上设置数据库架构。 该项目最初有两个不同的文件夹——默认的/Migrations 文件夹和另一个文件夹——带有 DbMi
我在我的 Nest 应用程序上运行这行代码: import * as typeOrm from "../node_modules/typeorm/index"; bootstrap(){ //
我需要像下面的代码一样创建一个外键: Create.ForeignKey().FromTable("TCGDocFiscalOpMedItem").ForeignColumn("IDCabecalho
如何从扩展 Migration 的类的 Up(或 Down)方法中获取迁移正在操作的数据库的名称 public class BaseMigration : Migration { public
我们已将迁移添加到我们的xamarin表单项目中,此后我们就无法将其部署到iOS设备上。它可以在模拟器上正常运行,但在部署到设备时会因AOT错误而失败。 这似乎是一个在网络上广泛传播的话题,但是到目前
我正在尝试自己研究 Fluent Migrator,但我遇到了一个问题,我需要更改由 Fluent Migrator 创建的表的结构,这意味着我使用以下代码创建了一个名为 user 的表 [Migr
我正在使用 PostgreSQL、Rails 3.1.3 和 Ruby 1.9.3。我正在努力使用 db:migrate 概述 here . 这是我在终端中看到的: funkdified@funkdi
我决定使用 gorm作为我的 ORM。我想使用 golang-migrate/migrate 进行迁移因为,看起来 GORM 没有版本化迁移文件。我宁愿使用 CLI 进行迁移,而不是使用自动迁移。 我
我正在针对现有 SQL Server 数据库设置 EF Core 3.1.3 迁移。 这是我到目前为止所做的: 从现有数据库搭建我的模型。 添加 InitialCreate 迁移。 删除 Initia
我正在玩 Entity Framework 和持续构建。到目前为止,通过使用 migrate.exe 和适当的参数,我能够毫无问题地运行迁移或一系列迁移。 但是,我在尝试让 migrate.exe 踢
我正在使用包管理器控制台向我的数据库添加迁移,但由于某种原因它只是卡住并且什么也不做。停止它并继续使用它的唯一方法是使用任务管理器关闭 Visual Studio。这是一个已知问题吗? 最佳答案 我不
我在sequelize上运行帮助命令,发现有两个不同的命令具有相同的描述: $ sequelize help:migration:create Sequelize [Node: 6.9.5, CLI:
在这里,我使用的是 Microsoft Azure。 其中,我正在尝试使用开源 DocumentDB 数据迁移工具将数据从各种来源导入 Microsoft Azure DocumentDB,包括 JS
我是 Laravel 的新手。 我正在开发 Laravel 版本 6。 我已经创建了迁移。 第一次运行良好,但如果我更改迁移文件中的某些内容,然后运行 php artisan migrate它显示
我已经使用 NuGet 安装了 Migrator.NET,但是当我尝试使用 MSBuild 运行迁移时,出现以下错误。我看到它在访问 Migrator.Framework 程序集时遇到问题,但我不确定
我有一个基于 miguel flask tutorial 的网络应用程序 所以我将 sqlalchemy-migrate 用于数据库,现在我应该在迁移中使用 Alembic 我该如何配置?有什么方法可
我已经尝试执行 cli 命令 ./doctrine generate-migrations-diff 并在正确的文件夹中正确创建了一个版本文件。 消息是:generate-migrations-dif
可以定义存储迁移的多个路径: doctrine_migrations.yaml doctrine_migrations: migrations_paths: 'App\Migr
我在我的 .Net 应用程序中使用 Fluent Migrator 1.6.2。 对于数据库的上迁移或下迁移,需要通过命令行传递汇编文件(迁移类所在的数据库工程的dll文件)来执行Migrate.ex
我是一名优秀的程序员,十分优秀!