Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDarren Burges
4,453 Pointsuniqueness validation in sql pro
it seems that the uniqueness validation is not working. Per the migration code:
public function up()
{
Schema::create('todo_lists', function(Blueprint $table)
{
$table->increments('id');
$table->string('name')->unique;
$table->timestamps();
});
}
the 'name' column should be required to be unique. Note however that trying to enter the same value in two different rows does not throw an error, either in sequel pro nor in the laravel app itself.
Why does this column not get provided a uniqueness property when I run the migration?
3 Answers
Petros Sordinas
16,181 PointsDarren,
You forgot to add the () after unique. It is a method, not a property :)
Darren Burges
4,453 PointsOk, thanks for that. So why is there no error during the migration?
Petros Sordinas
16,181 PointsI'm not sure why no error is thrown. In theory, you should have seen a "Notice: undefined method" error. Maybe artisan has set error_reporting to not show notices...
thomascawthorn
22,986 PointsI think you'd only get an undefined method error if the method didn't exist - but Darren was calling a property not an undefined method. I think calling an undefined property will result in a warning by default (but I could be wrong!) so no exception would be thrown.
If you know $table is an instance of Blueprint, you always dive right into the Blueprint class and find out if there's a property called unique ;)