最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - When loading data with seeder in Laravel, it says there is no conversion from nvarchar data type to datetime data type - S

programmeradmin3浏览0评论
public function up(): void
    {
        Schema::create('books', function (Blueprint $table) {
            $table->id();
            $table->string('book_name');
            $table->string('author');
            $table->integer('page_count');
            $table->foreignId('category_id')->constrained()->onDelete('cascade');
            $table->string('isbn');
            $table->string('publisher');
            $table->string('publish_year');
            $table->string('status');
            $table->timestamps();
        });
    }

SQLSTATE[22007]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. (Connection: sqlsrv, SQL: insert into [books] ([book_name], [author], [page_count], [category_id], [isbn], [publisher], [publish_year], [status], [updated_at], [created_at]) values (1984, Gee Orwell, 328, 4, 9780451524935, Penguin Books, 1949, available, 2025-03-17 14:01:33.722, 2025-03-17 14:01:33.722))

I am working on a website but I always get the same error when trying to add data with seeder. I tried using datetime instead of timestamps and although I made it nullable, it continues to give the same error. I use SQL Server Management Studio.

public function up(): void
    {
        Schema::create('books', function (Blueprint $table) {
            $table->id();
            $table->string('book_name');
            $table->string('author');
            $table->integer('page_count');
            $table->foreignId('category_id')->constrained()->onDelete('cascade');
            $table->string('isbn');
            $table->string('publisher');
            $table->string('publish_year');
            $table->string('status');
            $table->timestamps();
        });
    }

SQLSTATE[22007]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. (Connection: sqlsrv, SQL: insert into [books] ([book_name], [author], [page_count], [category_id], [isbn], [publisher], [publish_year], [status], [updated_at], [created_at]) values (1984, Gee Orwell, 328, 4, 9780451524935, Penguin Books, 1949, available, 2025-03-17 14:01:33.722, 2025-03-17 14:01:33.722))

I am working on a website but I always get the same error when trying to add data with seeder. I tried using datetime instead of timestamps and although I made it nullable, it continues to give the same error. I use SQL Server Management Studio.

Share Improve this question edited Mar 17 at 14:19 Ahmet Faruk Yaşar asked Mar 17 at 14:16 Ahmet Faruk YaşarAhmet Faruk Yaşar 112 bronze badges 10
  • Your datetime value looks to be in an ambiguous format for datetime; I suspect it is being read as a yyyy-dd-MM value. Considering the value as well (end with .722) I would suggest that datetime is the wrong data type for several reasons, including it's not accurate enough (datetime cannot stored the value 2025-03-17 14:01:33.722 and would round it to the nearest 1/300th of a second). Have you considered using a datetime2 with an appropriate precision? – Thom A Commented Mar 17 at 14:21
  • This question is similar to: Error converting nvarchar to datetime data type using Laravel and MSSQL. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – siggemannen Commented Mar 17 at 14:42
  • ODBC Driver 17 for SQL Server is very old. Update to 18. The date-related database types have no format either, they're binary values. Instead of trying to convert them to strings specify the timestamp precision, eg $table->timestamps(2);. That's the real fix in the related Laravel issue – Panagiotis Kanavos Commented Mar 17 at 14:51
  • @PanagiotisKanavos it seems kinda weird that timestamp precision solves anything. Could it be more luck than something else? – siggemannen Commented Mar 17 at 17:34
  • 1 @siggemannen that's absolutely correct, it IS brittle. – AlwaysLearning Commented Mar 17 at 21:56
 |  Show 5 more comments

1 Answer 1

Reset to default 0

I would consider another solution:

First make the published_year an integer type, like this:

$table->integer('publish_year');

Then for some i would have tried to add both created_at and updated_at manually in the seeder:

'created_at' => now(),
'updated_at' => now(),

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论