While designing the database for a laravel software using MYSQL, is assigning foreign keys relevant or does Laravel take care of that "Software side".
In the migration we have something like
Table Example:
$table->unsignedBigInteger('user_id');
should i modify the example table in phpmyadmin and make user_id a foreign key? is there a better way or is this not relevant or necessary?
You should define foreign key constraints in your migration. When using code base you should make all the changes using migrations.
Additionally by defining foreign key you actually build a relation between 2 tables otherwise this relation will be at code level. When relation is built database will restrict to have only values which actually exists in main table. Using foreign key you can also do cascading (on update and delete) at db level.
Reference what are the advantages of defining a foreign key
Why should I use foreign keys in database?
if use Laravel 7 ,you can use this short that is a column name user_id foreign to ID user in the table users :
$table->foreignId('user_id')->nullable()->constrained()->onDelete('cascade');
Related
I have some query related to Eloquent Relationships in Laravel.
If I didn't specify a foreign key when using any relationship defined by laravel so laravel makes it work by assuming the foreign key based upon the parent model name as written in laravel 8 documentation.
So what I want to ask is that if laravel is only assuming means laravel will make the relationship work but data consistency will not be maintained by laravel as what foreign key does? Am I right?
And if I am right then the solution will be to explicitly define the foreign key in migration file when using relationships?
I guess, you're mixing two things, the database's DDL/schema and the ORM/database abstraction layer:
The database's DDL/schema defines, how the database itself is structured and works. In Laravel, this can be controlled via migrations.
The ORM/database abstraction layer normally doesn't alter the schema, it just does CRUD (which is DML not DDL). In Laravel, this can be done with either the query builder or Eloquent, which is an ORM.
That means, the former creates the foreign key constraints, the latter only assumes a column name to put in the SQL query it creates
I am currently doing ZF2 project with Doctrine2. I have existing database where tables does not have foreign key constraints and any relation. Using doctrine can I generate schema based on relation without foreign key constraints.
I tried to add the column without foreign key constraints and its violated. Also I tried the column null able still doctrine try to modify the db schema to add foreign key constraints which eventually fails.
Also tried to add another temporary column as foreign key and later on write a script top transfer existing column to the foreign key constraint column. But it also failed as there have some columns who does not exist in the second table( may be deleted)
still does not find any suitable way to do that.
Any idea? or its not possible?
When i need to set a field as the foreign key in a table,in PHPMyAdmin. I am not getting it set right.
There is a Parent table called 'user' which has a primary key called 'uid'.I am using the 'uid' in the child table called 'student_register' as foreign key. But while setting it as foreign key constraint through the relation view link in phpmyadmin . i am not able to see the 'user' table in the drop down list to select it and set the 'uid' as foreign key .. I have sent the screen shot to get a clear picture.
the screenshot for the phpmyadmin child table 'student_register'
From the MYSQL User Guide:
If you are facing this issue than you need to follow these basic steps:
Database storage engine must be InnoDB.
Your relational tables must be InnoDB.
Use UNIQUE reference key.
Reference Key can not be NULL.
Datatype of the both columns must be same.
References:
Create Table Foreign Keys in MYSQL
Creating Foreign Key Relationships
You should add index to the column uid in table user in order to appear in the drop down list
Take the structure of table user, then click the last option of the Action - Add index for the column uid.
I get that foreign key constraints great for integrity of a database and all, but it's also a huge overhead to use constraints when dealing with tables that are in the millions and growing.
I want to remove foreign key constraints from my application. In past symfony2 projects I've removed constraints manually, but I'm assuming maybe there is a way to simply tell symfony2 to do this?
If anyone is aware of a way to do this within the framework please let me know :)
edit:
Let's say for example in a manyToMany relationship, it auto-creates the relationship table with the proper indexes but it also puts a foreign key constraint on both columns as well, or if i have a oneToMany relationship it puts a foreign key constraint on that relationship.
I don't want these foreign key constraints to be created.
Found in Doctrine's JIRA:
You can disable the exporting of foreign keys for specific models:
User:
attributes:
export: tables
columns:
or with php:
$userTable->setAttribute(Doctrine::ATTR_EXPORT, Doctrine::EXPORT_TABLES);
Now it will only export the table definition and none of the foreign keys. You can use: none, tables, constraints, plugins, or all.
I'm using doctrine2 with a symfony2.1 project. I have an entity that has a few many to one relationships to other tables. The foreign key relationships for these many-to-one's have already been updated in the database, but every time I run migrations:diff or schema:update --dump-sql it adds the same update commands to add the foreign key relationships again. When I run schema:validate it says my mapping is out of sync with my database.
My application works fine, the relationships are working properly, and the schema in my database looks correct. Why is doctrine still trying to add these foreign keys?
Here's my code (for one of the problematic parameters):
In my "Ticket" entity I have:
/**
* Authenticated User who scored the ticket.
*
* #ORM\ManyToOne(targetEntity="CS\SecurityBundle\Entity\User")
* #ORM\JoinColumn(name="scoring_user_id", referencedColumnName="id")
*/
protected $scoringUser;
I currently have it set up to be one-directional, so there is no inversedBy in the User entity.
This generates the following in my migrations or schema:update dump even though it's in the database already:
$this->addSql("ALTER TABLE tickets ADD CONSTRAINT FK_54469DF4BB0D9452 FOREIGN KEY (scoring_user_id) REFERENCES users (id)");
Any idea what I'm doing wrong here?
Why is doctrine still trying to add these foreign keys?
The correct term here is "foreign key constraint". Without the constraint, the column in question is just a column. It's the constraint that enforces that the value of that column exists as a primary key in another table.
Why is doctrine still trying to add these foreign keys?
Because the database vendor/engine doesn't support foreign key constraints, but Doctrine fails to recognize that.
If I have to guess, you're using MySQL with the MyISAM engine. MyISAM doesn't support foreign key constraints. Unfortunately Doctrine isn't "smart" enough to see that. It sees that MySQL is used, therefor blindly assumes that foreign key constraints are supported.
My advise is to switch to the InnoDB engine, unless you have a good reason for using MyISAM.
ALTER TABLE table_name ENGINE=InnoDB;
Converting Tables from MyISAM to InnoDB