Foreign Key
See original GitHub issueThanks for a really nicely implemented project. Annotated source code is always a deity-send. I have a few questions:
Is there a way to create foreign keys to other tables? The source code has a function foreign
with the comment “Specify a foreign key for the table.” within the SchemaBuilder
, but there doesn’t seem to be a way to specify a table and column for the foreign key, and the documentation doesn’t mention anything.
Is there a way to specify that two or more columns must be unique together?
Is there a way to define indexes on non-integer columns?
Thanks!
Issue Analytics
- State:
- Created 10 years ago
- Comments:16 (8 by maintainers)
Top Results From Across the Web
SQL FOREIGN KEY Constraint
A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY KEY in another table. The...
Read more >Foreign key
A foreign key is a set of attributes in a table that refers to the primary key of another table. The foreign key...
Read more >What is a foreign key? (With SQL examples)
A foreign key column in a table points to a column with unique values in another table (often the primary key column) to...
Read more >What is a foreign key? - Definition from TechTarget
A foreign key is a column or columns of data in one table that refers to the unique data values -- often the...
Read more >SQL - Foreign Key
A foreign key is a key used to link two tables together. This is sometimes also called as a referencing key. A Foreign...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Edit: jtremback has this in his above as well.
In order to get this work, I had to do the following:
Where parentid is the field in the child table that should reference the field id in the parent table.
The unsigned part in step 3 was key. MySql wouldn’t create the foreign key if the type didn’t match, and it also seemed to silently fail in that case, though that could be a problem with my error handling.
You might want to take a look at #148 for an example of how I do migrations. Your best bet is to create tables and their primary keys / regular columns in one migration and then add the foreign key columns in a second one. Any other way creates a huge mess.