Giter Club home page Giter Club logo

Comments (6)

ddelnano avatar ddelnano commented on July 28, 2024

What do your database tables look like? Your having some foreign key constraint fail probably

from testdummy.

sithuaung avatar sithuaung commented on July 28, 2024

Thanks @ddelnano
but I don't think my database is wrong coz I can make it work with normal database seeder. (not with model factory). for example. when I save [ user_id ] to posts table it saves like [ user_id = factory:App\User ]. I'm on Laravel v 5.1.16.

from testdummy.

ddelnano avatar ddelnano commented on July 28, 2024

Unless you show the tables and migrations we won't be able to tell.

from testdummy.

sithuaung avatar sithuaung commented on July 28, 2024

hi @ddelnano

I have Goods table & Imports table and others table similar like that.

// Goods Schema ...

        Schema::create('goods', function(Blueprint $table) {
            $table->increments('id');

            $table->integer('import_id')->unsigned();
            $table->foreign('import_id')->references('id')->on('imports');

            $table->integer('car_type_id')->unsigned();
            $table->foreign('car_type_id')->references('id')->on('car_types');

            $table->integer('brand_id')->unsigned();
            $table->foreign('brand_id')->references('id')->on('brands');

            $table->timestamp('model_year');

            $table->string('engine_power');
            $table->string('consignor_name')->nullable();
            $table->decimal('unit_price', 12, 2); // 100,000,000

            $table->integer('permit_type_id')->unsigned();
            $table->foreign('permit_type_id')->references('id')->on('permit_types');

            $table->string('port_of_landing');

            $table->integer('import_method_id')->unsigned();
            $table->foreign('import_method_id')->references('id')->on('import_methods');

            $table->timestamps();
        });

// and Import Table ....

        Schema::create('imports', function(Blueprint $table) {
            $table->increments('id');
            $table->integer('voucher_no');
            $table->timestamp('licence_date');
            $table->decimal('fees', 12, 2); // 100,000,000
            $table->string('licence_no');
            $table->timestamp('last_date_import');
            $table->integer('registration_no');
            $table->integer('hs_code');
            $table->timestamps();
        });

Bty thanks for ur time.

And my full error message is...

  [Illuminate\Database\QueryException]                                                                                              
  SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`account  
  ing`.`goods`, CONSTRAINT `goods_import_id_foreign` FOREIGN KEY (`import_id`) REFERENCES `imports` (`id`)) (SQL: insert into `goo  
  ds` (`import_id`, `car_type_id`, `brand_id`, `model_year`, `engine_power`, `consignor_name`, `unit_price`, `permit_type_id`, `po  
  rt_of_landing`, `import_method_id`, `updated_at`, `created_at`) values (factory:App\Import, factory:App\CarType, factory:App\Bra  
  nd, 2015-09-14 13:37:03, 1032cc, Priscilla Reichert MD, 1316, factory:App\PermitType, Israel, factory:App\ImportMethod, 2015-09-  
  14 13:37:03, 2015-09-14 13:37:03))  

[PDOException]                                                                                                                    
  SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`account  
  ing`.`goods`, CONSTRAINT `goods_import_id_foreign` FOREIGN KEY (`import_id`) REFERENCES `imports` (`id`)) 

And my database seeder file is

$this->call('ImportsTableSeeder');
factory(App\Good::class, 50)->create();

from testdummy.

ddelnano avatar ddelnano commented on July 28, 2024

Yea so the import_id is not getting set when you are trying to seed the database you need in your factory definition

$factory('Good`, [
// other columns....
     'import_id' => 'factory:Import'
]);

from testdummy.

sithuaung avatar sithuaung commented on July 28, 2024

Yeah I've test with

'import_id' => 'factory:Import',

// and 

'import_id' => 'factory:App\Import',

both not work.

from testdummy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.