question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Thanks 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:closed
  • Created 10 years ago
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
davethesoftwaredevcommented, Mar 19, 2014

Edit: jtremback has this in his above as well.

In order to get this work, I had to do the following:

  1. Create the parent tables first, then create the child tables (parallel issue mentioned above)
  2. I’m using table.increments(‘id’) to create the primary key of the parent tables, and this creates them as unsigned auto_increment integers.
  3. To have it actually create the foreign key, I had to something like:
table.integer('parentid').unsigned().references('id').inTable('parent'); 

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.

2reactions
bendruckercommented, Jan 8, 2014

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found