Laravel数据库填充:如何更改已填充列的默认名称?

huangapple go评论74阅读模式
英文:

Laravel Database Seeding: How to change the default name of the seeded column?

问题

create_users_table中,我将列的名称从'name'更改为'username'。然而,在设置数据库填充器时,我收到以下错误信息:

SQLSTATE[42S22]: 列未找到:1054 未知列'name'在'field list'中(连接:mysql,SQL:插入到users中(nameemailemail_verified_atpasswordremember_tokenupdated_atcreated_at

这是DatabaseSeeder中的行:
$user = User::factory()->create();

我尝试更改了User模型中的fillable属性,但仍然遇到相同的问题。我不知道应该更改哪个文件。

英文:

In the create_users_table I changed the name of the column from 'name' to 'username'. However while setting up the database seeder I get the following error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name' in 'field list' (Connection: mysql, SQL: insert into users (name, email, email_verified_at, password, remember_token, updated_at, created_at)

This is the line in the DatabaseSeeder:
$user = User::factory()->create();

I tried changing the fillable property in the User model but still same problem. I do not know which file I should change.

答案1

得分: 0

请确保您的数据库具有正确的列名,并尝试在种子示例中插入完整的对象:

DB::table('users')->insert([
    'username' => 'username',
    'email' => 'myemail@email.pt',
    'password' => Hash::make('mypassword')
]);

我正在使用QueryBuilder,但您始终可以在Eloquent和您的模型中执行此操作。

英文:

Be sure your database has the right column name and try to insert the full object in the seeder example:

DB:table('users')->insert([
'username'=> 'username',
'email' => 'myemail@email.pt',
'password' => Hash:make('mypassword')
]);

I am using QueryBuilder, but you can always do it Eloquent and your models.

答案2

得分: 0

我认为你正在搜索名为:
UserFactory.php
的文件

它应该位于路径:
\database\factories\

如果你找不到它,我建议检查该目录中是否有其他类似名称的文件。

要刷新数据库迁移,请使用以下命令:

php artisan migrate:refresh

要填充数据库,请使用以下命令:

php artisan db:seed

如果在迁移过程中出现错误,你可以尝试删除migrations表,然后重新运行迁移命令。

请注意,运行php artisan migrate:refresh命令将重置你的数据库并删除所有现有数据。

英文:

I think you are searching for a file named:
UserFactory.php

it should be located at the path:
\database\factories\

if you cannot find it, I recommend checking for any other files with similar names in that directory.

To refresh the database migration:

php artisan migrate:refresh

To seed the database:

php artisan db:seed

If you get an error during the migration process, you can try removing the migrations table and then re-running the migration command.

Please be aware that running the php artisan migrate:refresh command will indeed reset your database and remove all existing data.

huangapple
  • 本文由 发表于 2023年6月19日 01:04:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76501685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定