为什么 - > save();没有工作。错误是外国钥匙违规

发布于 2025-02-07 12:15:33 字数 704 浏览 2 评论 0原文

我正在尝试更新模型,并且保存编辑时,我会遇到错误“外键违规,键(country_code)=(hun)不存在在“乡村”表格中,这是正确的。我希望密钥“ country_code”在国家 /地区的密钥“代码”上引用。

我的餐桌协会在其钥匙“代码”上有一个外国_KEY“ country_code”。

我的社会迁移有这样的界限:

$table->foreign('country_code')->references(Country::getPrimaryKeyName())->on(Country::getTableName())->onDelete('set null');

我的实体有这样的行为:

public function country()
  {
    return $this->belongsTo(Country::class, 'country_code', Country::getPrimaryKeyName());
  }

我检查了国家的主要关键确实是“代码”。

我知道这并不真正尊重Laravel的标准,但是请相信我,如果我有选择,我会尊重它们,但是在这里我以这些奇怪的钥匙名称。

社会中的钥匙“ country_code”和国家 /地区的“代码”是字符串。

谢谢您,如果您可以帮助我。 如果您需要更多信息,我会很乐意为您提供任何帮助。

I'm trying to update a model and when i save the edit, I get the error "Foreign key violation, The key (country_code)=(HUN) is not present in the "country" table" which is true. I want the key "country_code" to reference on the key "code" on Country.

I have my table society that has a foreign_key "country_code" that reference on the table Country on its key "code".

My society migration has a line like that :

$table->foreign('country_code')->references(Country::getPrimaryKeyName())->on(Country::getTableName())->onDelete('set null');

My entity has this :

public function country()
  {
    return $this->belongsTo(Country::class, 'country_code', Country::getPrimaryKeyName());
  }

I checked that the country primary key on Country is indeed "code".

I know that it doesn't really respect laravel standard but believe me, if I had the choice, I would have respected them but here I am with these strange keys name.

The keys "country_code" in Society and "code" in Country are string.

Thank you if you can help me with this.
If you need more information, I'll gladly give you whatever can help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

-黛色若梦 2025-02-14 12:15:33

问题在于,在我的列“乡村代码”列中,我所有的代码都在他们之后(例如Hun,IDN)之后都有一个空间,而且我没有看到该空间,因此没有匹配。

我问题的答案是,确实与我想要的东西没有匹配。我的专栏中没有“ unn”的“乡村”代码,因为只有“匈奴”。

The problem was that in my column "code" of "country", all my codes had a space after them (for example HUN , IDN ) and I didn't saw the space so it had no match.

The answer to my question was that there was indeed no match to what I was looking for. There was no "HUN" in my column "code" of "country" because there was only "HUN ".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文