資料表一對一關聯問題

來源:互聯網
上載者:User
class User extends Authenticatable{    public function profile()    {        return $this->hasOne(Profile::class);    }}
class Profile extends Model    public function user()    {        return $this->belongsTo(User::class);    }    }

上面是一張user表和profile表的Model,兩張表一對一關聯性,當使用者登入後,可以建立他的profile,然後提交。
可是,第二次訪問這個建立profile的表單頁,也能提交成功,這哪裡是一對一,這不是一對多麼!是哪裡有錯,還是模型中進行一對一關聯後,要保證profile只有一條記錄,在控制器中還要進行判斷?

回複內容:

class User extends Authenticatable{    public function profile()    {        return $this->hasOne(Profile::class);    }}
class Profile extends Model    public function user()    {        return $this->belongsTo(User::class);    }    }

上面是一張user表和profile表的Model,兩張表一對一關聯性,當使用者登入後,可以建立他的profile,然後提交。
可是,第二次訪問這個建立profile的表單頁,也能提交成功,這哪裡是一對一,這不是一對多麼!是哪裡有錯,還是模型中進行一對一關聯後,要保證profile只有一條記錄,在控制器中還要進行判斷?

要是能建立多個,那就不是一對一了唄
資料庫是人設計的,又不是死的,需求是什麼樣的就設計成什麼樣的,如果是一對一,那就不能提交兩個,如果能提交多個,那就是一對多

提交後是重新覆蓋那一條資料就可以一對一咯。

首先查庫看是否在profile已經有 該使用者的記錄了,如果有則update 更新 否則 insert插入記錄

這個是你業務設計的問題,你需要在業務中去控制,如果出現了不符合一對一的情況,可以認為出現了資料不一致。

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.