I have Playlist and Track Many To Many relationship with additional order field:
Schema::create('playlist_track', function (Blueprint $table) {
$table->integer('playlist_id')->unsigned();
$table->integer('track_id')->unsigned();
$table->integer('order')->unsigned();
$table->primary(['playlist_id', 'order']);
});
User can delete tracks from playlists and change an order. So this sync method is what I need:
foreach ( $tracks as $key => $track ){
$_tracks[ $track ] = [ 'order' => $key ];
}
$playlist->tracks()->sync( $_tracks );
But I get an error when trying to change an ordering of tracks:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '3-0' for key 'PRIMARY' (SQL: update `playlist_track` set `order` = 0 where `playlist_id` = 3 and `track_id` = 1)
That's because i'm using composite primary key I guess... But I don't know how to make it works
UPD
This is dd( $_tracks )
array:2 [
1 => array:1 [
"order" => 0
]
3 => array:1 [
"order" => 1
]
]
Since it's a pivot table, you can't define foreign key as primary, so remove this:
$table->primary(['playlist_id', 'order'])
Pivot table can have non unique playlist_id values, but with ->primary() you're creating a constraint.
Use an auto-increment, integer column as a primary key:
$table->increments('id');
Related
Duplicates:
Laravel 8 Unique form validation ignore
following this doc
This is my request validation:
public function rules()
{
return [
'LFNumber' => ['required', 'integer', Rule::unique('lost_and_found', 'id')->ignore($this->id, 'id')],
];
}
I'm trying to edit some fields of the form but I either get LFNumber already exist or SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'LFNumber'
I tried Rule::unique('lost_and_found', 'LFNumber')->ignore($this->id, 'id') to select the LFNumber column and ignore the id. But still he same errors.
If the unique column is LFNumber then you don't need to provide it:
'LFNumber' => ['required', 'integer', Rule::unique('lost_and_found')->ignore($this->id)],
This will check lost_and_found.LFNumber column while ignoring row with Primary Key matching $this->id (assuming this value is really the id on that column)
I'm trying to seed data into tags table my schema is as follows:
Schema::create('tags',function (Blueprint $table){
$table->bigIncrements('id');
$table->string('tag_name');
});
my seed is as follows:
public function run()
{
$array=array(
array(
'computer'
),
array(
'hp'
),
array(
'mac'
)
);
DB::table('tags')->insert($array);
}
The array of data to insert into the database is an associative array where the keys are the columns to be set with corresponding values:
['field' => $value, 'other_field' => $otherValue]
If you bulk inserting you will have an array containing arrays of values to insert:
[['tag_name' => 'computer'], ['tag_name' => 'hp'], ...]
Currently you have zero indexed arrays:
[[0 => 'computer'], [0 => 'hp'], ...]
So it is trying to set a column named 0.
Laravel 6.x Docs - Query Builder - Inserts
I have a table that's defined thus:
Schema::create('tableA', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('label_id')->unsigned();
$table->date('date');
$table->integer('value');
$table->unique(['user_id','label_id','date']);
$table->timestamps();
});
The table uses a composite key of user_id, label_id and date.
I would like to update the table using the Model::updateOrCreate method thus:
Model::updateOrCreate(
[
'user_id' => $user_id,
'label_id' => $label_id,
'date' => $date,
'value' => $value,
]);
But I get an error if I run the method when a row with the composite key already exists because it seems Laravel doesn't work with composite keys.
For example row
[
user_id:2,
label_id:3,
date:'2019-04-04',
value: 44
]
cannot be updated using
Model::updateOrCreate([
user_id:2,
label_id:3,
date:'2019-04-04',
value: 100
]);
Does this mean there's no way to use updateOrCreate and I need to check each row if it exists before I attempt to add it ?
If you want to update a row based on a user_id the first parameter of the updateOrCreate is an array of fields you are looking to match, and the second contains fields that should update. So from what you are saying I believe you should use this:
Model::updateOrCreate([
user_id => 2,
label_id => 3,
date => new Carbon('2019-04-04')
],
[
value => 100
]);
I am trying to insert an array with date field - end_date in postgresql with Laravel 5.7. But this gives an error -
SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column \"end_date\" violates not-null constraint
I am using Laravel 5.7, Postgresql 9.3
$array = [
"amazon_store_id" => 4
"advertising_profile_id" => 1
"campaign_id" => 123
"name" => "6 shelf 2"
"campaign_type" => "sponsoredProducts"
"targeting_type" => "manual"
"premium_bid_adjustment" => true
"daily_budget" => 15.0
"start_date" => "2014-11-25 09:32:18"
"end_date" => null
"state" => "paused"
"serving_status" => "CAMPAIGN_PAUSED"
"creation_date" => "2014-11-07 10:17:03"
"last_updated_date" => "2018-10-24 12:49:54"
"created_at" => "2018-12-24 09:32:18"
"updated_at" => "2018-12-24 09:32:18"
];
DB::table($table_name)->insert($array->toArray());
Ideally it shall insert the null in the database.
In your migration you can do this to make the column nullable:
public function up()
{
Schema::create('tablename', function (Blueprint $table) {
$table->dateTime('end_date')->nullable();
});
}
->nullable() Designate that the column allows NULL values
In your migration file you have to define the date field as nullable.
Schema::table($table_name, function (Blueprint $table) {
$table->dateTime('end_date')->nullable();
});
I'm developing an October CMS Plugin for managing animals (with rainlab.builder).
Animals have a couple of fields and relations. Every animal have a father and a mother animal. But when I try to safe my animal the following error appears:
Event Log:
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048
Column 'id' cannot be null in dir/website/vendor/laravel/framework/src/
Illuminate/Database/Connection.php:413
Plugin animal form:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'id'
cannot be null (SQL: update `prefix_animals_animal` set `id` = where
`prefix_animals_animal`.`id` = 1 and `prefix_animals_animal`.`id` is
not null)" on line 666 of dir/website/vendor/laravel/framework/
src/Illuminate/Database/Connection.php
The error only appers when I use the relations (child -> father, child -> mother).
Code
I have implemented the following hasOne - relations to my animal model:
/* Relation */
public $hasOne = [
'father' => [
'Namespace\Animals\Models\Animal',
'key' => 'id',
'otherKey' => 'id'
],
'mother' => [
'Namespace\Animals\Models\Animal',
'key' => 'id',
'otherKey' => 'id'
]
];
This are my fields from field.yaml:
father:
label: Father
oc.commentPosition: ''
nameFrom: name
descriptionFrom: description
emptyOption: 'No father'
span: left
type: relation
mother:
label: Mother
span: right
oc.commentPosition: ''
nameFrom: name
descriptionFrom: description
emptyOption: 'No mother'
type: relation
I would be very happy if someone have a solution for these kind of relations.
Cheerio!
Don't use id as the primary key for the relation as this is not good practice.
Actually this creates the problem you are seeing also. Currently your model is using the id for primary Id, mother Id and father Id. You can't do that.
Add mother_id and father_id to the Animal model and change the relation definition to this:
public $hasOne = [
'father' => [
'Namespace\Animals\Models\Animal',
'key' => 'father_id',
'otherKey' => 'id'
],
'mother' => [
'Namespace\Animals\Models\Animal',
'key' => 'mother_id',
'otherKey' => 'id'
]
];
PS. In your case you dont have to define key and otherKey as the default value for otherKey is "id" and the otherKey value is created from the relation name with the "_id" suffix.