nothingworksinc / ticketbeast

Back to the lesson videos:
https://course.testdrivenlaravel.com/lessons
552 stars 11 forks source link

Adding Tickets to Concerts - NOT NULL constraint failed #31

Closed naimkhalifa closed 7 years ago

naimkhalifa commented 7 years ago

Hi,

I'm following the 'Adding Tickets to Concerts' video but I'm facing an issue that doesnt show up in the course (around 4:00) :

Here's the error summary

Caused by PDOException: SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: tickets.concert_id

Here's my Concert class at that point:

`namespace App;

use Illuminate\Database\Eloquent\Model;

class Concert extends Model { protected $guarded = []; protected $dates = ['date'];

public function scopePublished($query)
{
    return $query->whereNotNull('published_at');
}

public function getFormattedDateAttribute()
{
    return $this->date->format('F j, Y');
}

public function getFormattedStartTimeAttribute()
{
    return $this->date->format('g:ia');
}

public function getTicketPriceInDollarsAttribute()
{
    return number_format($this->ticket_price/100, 2);
}

public function orders()
{
    return $this->hasMany(Order::class);
}

public function tickets()
{
    return $this->hasMany(Ticket::class);
}

public function orderTickets($email, $ticketQuantity)
{
    $order = $this->orders()->create(['email' => $email]);

    foreach (range(1, $ticketQuantity) as $i) {
        $order->tickets()->create([]);
    }

    return $order;
}

public function addTickets($quantity)
{
    foreach (range(1, $quantity) as $i) {
        $this->tickets()->create([]);
    }
}

public function ticketsRemaining()
{
    return $this->tickets()->count();
}

} `

and my migration for the tickets table:

`use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration;

class CreateTicketsTable extends Migration { /**

I think that the concert_id field should be automatically filled by Laravel thanks to the tickets() method but for some reason, it's not the case. Does anyone have a clue on this?

adamwathan commented 7 years ago

Hey @naimkhalifa just taking a look back at that lesson, are you running just the can_add_tickets test, or the whole file?

In the lesson, I'm just running that one test, so there's a good chance the other test in the file would also fail for me, and that we resolve that a little while after 😊

Let me know if that makes a difference, if not I'll spend some more time looking into it deeper and see if I can figure out what the difference might be.

naimkhalifa commented 7 years ago

Yes, that was it! I was running all the tests for ConcertTest and the error message was related to another test.

I wish using phpunit on Windows wasn't such a pain. As a reminder for anyone who might face the same "issue", I used phpunit filter option directly from the command line: ./vendor/bin/phpunit --filter can_add_tickets

By the way, thanks a lot for this amazing course! Getting into TDD feels a lot easier with this kind of resources.

Cheers

adamwathan commented 7 years ago

Great glad that solved it! Thanks for the great feedback, glad you are enjoying the course 👍