I have a sqlite database for which I need read only access in go. I have been exploring sqlboiler as an ORM and it's been great so far generating the models etc but 1 thing I was not able to figure out is how to define custom relations. I know that it does this automatically when the db has foreign keys etc but my db doesn't and I can't change its structure (it's written by another application). So I have a structure like this:
books:
id | title
1 | Sample title
authors:
id | name
1 | Author Name
book_authors:
book_id | author_id
1 | 1
I think it's fairly obvious what I want to do, this is a many to many relation between books and authors. The problem is that book_id and author_id are not foreign keys. Is there any way I can configure the code generation inside the toml file to create this relation or will I have to write code manually to do that? Thanks
Related
first image
second image
I want to get Id from students table where i want insert this id in differs table . I don't really know how to explain it but maybe if you see my codes, you would understand. I have already finish making the relationship between this 2 tables already
my student controller
this my differ controller but I do not know any idea about if this code true or no
first: 'app\Differ'? but Differ is in App\Models
you can use hasOne(Differ::class); when Student model and differ in the same folder
the second thing you should know, when using functions of relationship in laravel such as hasOne, a foreign key should be named by student_id in Differ or
you show it in function like hasOne(Differ::class,'id_student','id')
If the question is not StackOverflow-y I don't mind moderators to close it, I couldn't decide.
I have the following models: Users, Groups, Regions, Cities... There are like 4 of them. Every user can have multiple relations to each entity (administrator, manager, participant and some others)
Should I do a Relations table as follows:
user_id | target_id | target_type | relationship_type
1 | 2 | group | manager
And if so - how would I use Eloquent relations - HasManyThrough somehow?
Or is the Eloquent way something else? Maybe different tables for each entity (user_group, user_region) ?
The goal is to have each "Groups where Peter is admin in", "Regions where Jessica is participant" queried easily via Eloquent.
I need to record every day's information, like the food price, so I created almost infinite tables named with the same pattern as food_${date}, like food_20180222, food_20180223, and etc.
Each table has the same structure as follows
+----+-------+--+
| Id | Price | |
+----+-------+--+
| 1 | 10 | |
+----+-------+--+
I wonder that by using Spring JPA, is it possible to map one food entity to those tables with different names? Or can food entity be dynamically mapped to one of the tables determined by date?
i drug a line between two tables in the Linq Object Relational (O/R) mapper:
Order Customer
-------------- ---------------
| OrderID | | CustomerID |
| CustomerID |♦---------˃| ... |
| | | |
-------------- ---------------
Note: Or perhaps it was
Order Customer
-------------- ---------------
| OrderID | | CustomerID |
| CustomerID |˂---------♦| ... |
| ... | | |
-------------- ---------------
i'm not sure; it lets me drag both ways.
First question, what's the arrowhead, and what's the diamond?
Assuming the second diagram, the cardinality of the Association was created as OneToMany. This makes sense, since:
one customer
has many orders
But what confuses me is the Association.Unique (Boolean) property. It defaults to false. This makes sense because it's a OneToMany association. Order.CustomerID cannot be unique, otherwise it wouldn't be a OneToMany association, it would be OneToOne.
But then i'm allowed to change the OneToMany Unique property to true. This makes no sense, so i conclude that Unique ness doesn't apply to Order.CustomerID, but instead to Customer.CustomerID. But the diagram already indicates Customer.CustomerID is a Primary Key. Of course it's unique, it's a primary key.
But the Unique property isn't set. This makes no sense, so i conclude that Unique ness doesn't mean either table.
Second question, what does Unique mean?
Specifies whether the foreign target columns have a uniqueness constraint
Third question: What is parent and child?
Assuming, again, the second diagram:
Customers.CustomerID ♦------------> Orders.CustomerID
i take Customers table to be a parent. It's the one who owns what it means to be a customer. You want to change something about a customer, you walk to the parent.
Meanwhile, the child Orders table comes along and wants to reference a Customer.
Parent(diamond) Child(arrowhead)
==================== =================
Customers.CustomerID (PK) ♦------------> Orders.CustomerID (FK)
Except that when i look at the association's Parent and Child properties:
Child property
Name: Orders
Parent property
Name: Customer
They want to create a property on the "child" called Orders. No, no, no. The child is orders. And they want to add a property to the parent called Customer. No, no no. The parent is a customer.
That means i must have it backwards, and the terms parent and child are the exact opposite of what i thought:
Child(diamond) Parent(arrowhead)
==================== =================
Customers.CustomerID (PK) ♦------------> Orders.CustomerID (FK)
And by this time i want to blow my brains out; and instead spend 35 minutes authoring a question on Stackoverflow; rather than continuing to scream at my computer.
Help.
Regarding question 2 - The Unique property probably signifies that the relation is a OneToOne relation.
Even directly in a sql database there is no specific OneToOne relationship. Rather there is simply the foreign key where a key in one table can be exported to another table. As such a foreign key relationship is ALWAYS one to many. Using additional constraints on the table containing the exported key, the relationship can be made de facto one to one. But doing that doesn't change the foreign key relation, rather it simply applies additional conditions on the data in the foreign key table.
Regarding question 3: The property names mean that the child object (Order) will have a property called Customer which can be used to get an instance of the parent object. And the parent object will have a property called Orders which can be used to fetch children for the customer.
I need to build an application where there are generic entities (let's say articles, pages, nodes) where the user can add custom fields.
I've seen the approach that the most popular php CMS (wp, drupal) use to get this goal; They all have the base table with the minimum fields (e.g. title and body), and then delegates all the other fields to other tables, for example:
table node
id | title | body
table field_foo
node_id | field_type | field_value
table field_bar
node_id | field_type | field_value
// and so on
This, in a full mvc environment is pretty logic; The field controller handle every field separately.
But talking about performance, loading a single node will require many queryes - or many joins.
I've taken a different approach (even becose my app does not provides any base field): for every field i add a new column on the base table that will store a raw value, then a table for every field that need it (multiple fields for example, or reference to other entities) and an relation table with just indexes entity_id | field_id (that table actually do other kind of jobs, as keep track of versioningn and kind of relations between entities)
So with a single query i get all the raw data from an entity, then the field controller knows (when required) how and where to load the real values of that fields.
The column's type in the data table (table_entity_data) is the best guess for the field data: for text is text, for decimal is decimal; only for multiple fields (that have theyre value outside that table) is array (and the real data_type is in the _field_foo_value.entity_value_ column)
Assuming that the entity structure wont change often, i tryed to normalize the structure..
Given that other big projects handle this in a very different way I have come to doubt about my implementation, and wondering what kind of problem will happen with my hibryd structure:
table entity
id
table entity_data
entity_id | field_foo_rav_value | field_bar_raw_value
table relations
entity_id | entity_field_id | field_id_value
table field_foo_value
field_value_id | entity_value
// lets say field_bar is a single text field, there no will be another table:
// entity_data.field_bar_raw_value contains the real value
Any suggestion?
p.s: i know this question is kindly generic, feel free to flag for closing if not appropriate.
Looks like you're re-inventing EAV
http://www.google.com/search?q=entity+attribute+value+antipattern
Downsides are that you're throwing away all the type safety and structure that a relational database can provide.
In an ideal world, you probably want one of:
Allow the building of a proper table
Use a non-relational database