任何人都可以从以下错误消息“ errno:150”给我一个解决方案,外键约束的格式不正确“)”吗?当我想使用email属性创建从用户表到个人资料表的外键时,会出现问题,其中个人资料表中的主键是电子邮件本身。
表格用户
Schema::create('users', function (Blueprint $table) {
$table->bigInteger('id', 20)->autoIncrement();
$table->string('id_provider', 20)->nullable();
$table->string('provider_name', 255);
$table->string('email', 255)->unique();
$table->foreign('email', 255)->references('email')->on('profile')->onDelete('cascade');
$table->timestamp('email_verified_at')->nullable();
$table->string('password', 255)->nullable();
$table->string('role', 12);
$table->rememberToken();
$table->timestamps();
$table->primary('id');
});
模型用户
/**
* Get the profile record associated with user.
*/
public function profile()
{
return $this->hasOne('App\Profile');
}
塔贝尔个人资料
Schema::create('profile', function (Blueprint $table) {
$table->string('email', 255)->unique();
$table->string('name', 255);
$table->integer('phone_number', 12)->nullable();
$table->string('gender', 6)->nullable();
$table->integer('date', 2)->nullable();
$table->string('month', 9)->nullable();
$table->integer('year', 4)->nullable();
$table->bigInteger('id_address', 20)->nullable();
$table->string('avatar', 100);
$table->primary('email');
});
型号简介
/**
* Get the user that owns the profile.
*/
public function user()
{
return $this->belongsTo('App\User');
}
感谢您提供所有解决方案。
答案 0 :(得分:1)
public class SearchActivity extends AppCompatActivity implements
ContactsAdapter.ContactsAdapterListener {
...........
private SearchView searchView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
searchView.setFocusable(true);
searchView.requestFocusFromTouch();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
mAdapter.getFilter().filter(s);
searchView.clearFocus();
return false;
}
@Override
public boolean onQueryTextChange(String s) {
mAdapter.getFilter().filter(s);
return true;
}
});
}
您的用户模型:
<application
android:name="app.MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="activities.SearchActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
</application>
您的个人资料模型:
$table->foreign('email')->references('email')->on('profile');
答案 1 :(得分:1)
外键列为STRING unique(),引用列的类型为PRIMARY,而不是STRING。在个人资料表中,有两列名称相同但类型不同。
表配置文件
Schema::create('profile', function (Blueprint $table) {
$table->string('email', 255)->unique();
$table->string('name', 255);
$table->integer('phone_number', 12)->nullable();
$table->string('gender', 6)->nullable();
$table->integer('date', 2)->nullable();
$table->string('month', 9)->nullable();
$table->integer('year', 4)->nullable();
$table->bigInteger('id_address', 20)->nullable();
$table->string('avatar', 100);
$table->primary('email'); <--------You have a mix up here. The column should be $table->primary('id')
});