I'm using gorm to represents a model and do several operations.
The model contains an entity which has a one-to-many relationship with another entity an this has a one-to-many relatioship with another entity too.
Something link this:
+---------+ +------------+ +------------+
| entity1 | one-to-many | entity 2 | one-to-many | entyty3 |
| |------------->| entity1ID |------------->| entity2ID |
+---------+ +------------+ +------------+
When I run a complete update operation of the model the final state of postgres is inconsistent.
I haven't found info about gorm samples involving complex models. Most samples only contains one entity and a few samples contains models with two entities.
I'm starting to think that gorm isn't for complex projects and models.
How can I do an update operation with a consistent result in the storage?
Related
I am analyzing Laravel's backpack trial package, I am not sure about user role and permission.
I am seeing in database model_has_roles table
role_id | model_type | model_id
---------|---------------------------|----------
1 | App\Models\BackpackUser | 3
2 | App\Models\BackpackUser | 4
and model_has_permissions table
permission_id | model_type | model_id
--------------|--------------------------|-------------
1 | App\Models\BackpackUser | 3
2 | App\Models\BackpackUser | 4
I want to ask that what is model and how it is work?
Models are PHP classes that help you with database input/output, in a more object-oriented manner. It's how you interact with your database.
Please note you should have a reasonable understanding of Laravel before using Backpack. If you are not familiar with how Laravel works, you'll have a difficult time understanding how Backpack works. I recommend the free Laravel from scratch series on Laracasts.
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 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
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.