How to add pivot table to pivot table - eloquent

My DB is something like that : participant <=> event
I have a table "event_participant" to link the two others.
Now I want a new table : "Eat", that adds dinner's date for each day of an event for a participant.
I imagine table "Eat" like that : event_id, participant_id, date
But maybe it's better to do so : event_participant_id, date
Or maybe there's another way; you tell me.
I use Eloquent for Laravel 5.1 but any SQL answer could help.

Assuming your participant table
id | participant_fname | participant_lname | created_at | updated_at
| | | |
Assuming your event table
id | event_name | created_at | updated_at
| | |
Assuming your event_participant table
id | participant_id | event_id | created_at | updated_at
| | | |
As you have mentioned for the new Eat table, you could save the primary key of event_participant table. By doing so you could fetch the Event and Participant related details by doing a JOIN query. Both this table would be Pivot tables.
Eat table
id | event_participant_id | created_at | updated_at
| | |
Eloquent Relations
Laravel Relationships
Within your model you could then include the relationships between each tables to fetch values using Eloquent Relations
Hope this is helpful.

Related

Bulk update datatype of a column in all relevant tables

An example of some tables with the column I want to change.
+--------------------------------------+------------------+------+
| ?column? | column_name | data_type |
|--------------------------------------+------------------+------|
| x.articles | article_id | bigint |
| x.supplier_articles | article_id | bigint |
| x.purchase_order_details | article_id | bigint |
| y.scheme_articles | article_id | integer |
....
There are some 50 tables that have the column.
I want to change the article_id column from a numeric data type to a textual data type. It is found across several tables. Is there anyway to update them all at once ? Information schema is readonly so I cannot do an update on it. Other than writing inidividual alter statements for all the tables, is there a better way to do it ?

Tables referencing each other workaround

I have two tables (simplified):
+-------------+--------------+----------------------------+
| article |
+-------------+--------------+----------------------------+
| id | int | PRIMARY KEY AUTO_INCREMENT |
| author | int | FK (users) |
| created_at | date | |
| revision | int | FK (article_revision) |
+-------------+--------------+----------------------------+
+-------------+--------------+----------------------------+
| article_revision |
+-------------+--------------+----------------------------+
| id | int | PRIMARY KEY AUTO_INCREMENT |
| title | text | |
| content | text | |
| article_id | int | FOREIGN KEY (article) |
+-------------+--------------+----------------------------+
They are representing news feed articles. Every article can have multiple revisions (changes). I need somehow to specify a current revision from article table which is used (there can be pending revisions that can be used, only one at the time). But it would be circular references. Any workaround for this situation?
There is nothing that keeps you from adding circular foreign key references.
To deal with them, you only have to make sure that at the end of each statement or transaction, all constraints are satisfied:
If you use normal NOT DEFERRABLE constraints, you have to modify both tables in a single statement:
WITH current_revision AS (
INSERT INTO article_revision VALUES (...) RETURNING id
)
UPDATE article
FROM current_revision
SET revision = current_revision.id
WHERE ...;
With DEFERRABLE foreign keys, the check is deferred to the end of the transaction:
BEGIN;
INSERT INTO article_revision VALUES (...) RETURNING id;
UPDATE article SET revision = ... WHERE ...;
COMMIT;

Postgres: jsonb to jsonb[]?

Let's say I have this table:
ams=# \d player
Table "public.player"
Column | Type | Collation | Nullable | Default
-------------+--------------------------+-----------+----------+-------------------
id | integer | | not null |
created | timestamp with time zone | | not null | CURRENT_TIMESTAMP
player_info | jsonb | | not null |
And then I have this:
ams=# \d report
Table "public.report"
Column | Type | Collation | Nullable | Default
---------+--------------------------+-----------+----------+---------
id | integer | | not null |
created | timestamp with time zone | | not null |
data | jsonb[] | | not null |
How can I take the player_info from all the rows in the player table and insert that into a single row in the report table (into the data jsonb[] field)? My attempts with jsonb_agg() return a jsonb, and I can't for the life of me figure out how to go from jsonb to jsonb[]. Any pointers would be very much appreciated! Thanks in advance.
If you plainly want to copy the values, just treat it like any other data type, and use ARRAY_AGG.
SELECT ARRAY_AGG(player_info)
FROM player
WHERE id IN (...)
should return something of type json[].
Since jsonb[] is an array at the type level in PostgreSQL vs. a json array, use array_agg() instead of jsonb_agg().
insert into report
select 1 as id, now() as created, array_agg(player_info)
from player
;

Store an array field in a new table column

I have a table like this
| id | amenities | owner |
|----|--------------------------|----------------|
| 1 | {tv, hairdryer, iron} | Chris Houghton |
| 2 | {tv, aircondition, iron} | Matt Quinn |
I want to store the car table values in a new column "amenity_name" of another table "Amenity" but without UNNEST.
Here is what I tried so far
UPDATE public."Amenity" set amenity_name = (
SELECT amenities
FROM public."Listing" as l
cross join regexp_split_to_table(l.amenities , ',') as amenity_name
);
What is wrong with this code.

Changing name of `id` column in SphinxSearch index

I have created a SphinxSearch index which look like this:
+---------+-----------+
| Field | Type |
+---------+-----------+
| id | bigint |
| message | field |
| created | timestamp |
+---------+-----------+
Is there a way to run the indexer to change the name of the id column? I'm concerned about having multiple indexes all with a column called id. I would prefer to name it message_id or something more descriptive.
No. Id is a fixed name. Its the unique document id.
Can duplicate it into a unique attribute if you want