database-relations

Eloquent relationships laravel

我们两清 提交于 2020-05-19 03:59:27
问题 I have 3 tables categories id name categories_product id category_id product_id and a product table products id name I want products based on catergori_id, how kan i do that with laravel Eloquent? 回答1: Simple as this: Product::whereCategori_id($category)->get(); 来源: https://stackoverflow.com/questions/26920372/eloquent-relationships-laravel

Two 1 - N relations in Mongoid (Rails)

你。 提交于 2020-01-12 08:05:07
问题 The scenario is: How can an Account give ratings to another account? This results in two lists on the Account. Those who I have rated and those who have rated me. (my_ratings and ratings_given) This boils down to: How can multiple 1 - N relationsips to the same entity work in Mongoid? In Mongoid's Docs it says you can use has_many and belongs_to to link the entities together. I currently have this on Account has_many :ratings, :as => "my_ratings" has_many :ratings, :as => "ratings_given" and

Django: get related set from a related set of a model

南笙酒味 提交于 2020-01-03 08:43:27
问题 class Book(models.Model): # fields class Chapter(models.Model): book = models.ForeignKey(Book) class Page(models.Model): chapter = models.ForeignKey(Chapter) I want all the pages of the book A, possibly without cycling every chapter to fetch the pages. book = Book.objects.get(pk=1) pages = book.chapter_set.page_set #?!? 回答1: You can't do it that way. chapter_set is a query set, it doesn't have an attribute page_set. Instead, turn it around: Page.objects.filter(chapter__book=my_book) 回答2: When

Laravel 5 - Elequent GROUP BY is failing

那年仲夏 提交于 2019-12-24 11:43:01
问题 I am trying to do the following: I have two tables: 1) Content id, section_id parent_id, sequence, 2) Sections id, title, description, date_entered Each Content has to have a section, which is defined by a foreign key, the content can have a sub section, where if the content have the same parent_id - then this is classed as a sub section.. So for example: 1. My first section 1.1. My first sub section 2. My second section 3. My third section 3.1 My third sub section I am using Eloquent and

SQL database problems with addressbook table design

寵の児 提交于 2019-12-24 02:06:06
问题 I am writing a addressbook module for my software right now. I have the database set up so far that it supports a very flexible address-book configuration. I can create n-entries for every type I want. Type means here data like 'email', 'address', 'telephone' etc. I have a table named 'contact_profiles'. This only has two columns: id Primary key date_created DATETIME And then there is a table called contact_attributes. This one is a little more complex: id PK #profile (Foreign key to contact

How to use an auto incremented primary key as a foreign key as well?

人盡茶涼 提交于 2019-12-21 04:55:13
问题 This is what I'm trying to do: I have 2 tables... CREATE TABLE `parent` ( `id` int(11) NOT NULL AUTO_INCREMENT, `data` text, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; CREATE TABLE `child` ( `parent_id` int(11) DEFAULT NULL, `related_ids` int(11) DEFAULT NULL, KEY `parent_id` (`parent_id`), KEY `related_ids` (`related_ids`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; And then a constraint: ALTER TABLE `parent` ADD FOREIGN KEY (`id`) REFERENCES `child` (`parent_id`);

When to use 1-to-1 relationships between database tables?

十年热恋 提交于 2019-12-20 12:06:47
问题 A DB design question: when do you decide to use 1 to 1 relation tables? One of the places I see this is, for example, when you have a User and UserProfile table, people split them instead of putting all columns just in a User table. Technically, you can just put all the columns in one table since their relationship is 1-to-1. I know someone said that for the UserProfile table, over time you need to alter table to add more columns, but I really don't think this is a strong reason to split the

Delete all rows which has no id existing in another table

醉酒当歌 提交于 2019-12-18 06:48:54
问题 I want to delete all rows which has no existing foreign key in another table example: table1 +----+-------+ |id | data | +----+-------+ | 1 | hi | +----+-------+ | 2 | hi | +----+-------+ | 3 | hi | +----+-------+ | 4 | hi | +----+-------+ | 5 | hi | +----+-------+ table2 +----+-------+ |a_id| data | +----+-------+ | 1 | hi | +----+-------+ | 20 | hi | +----+-------+ | 3 | hi | +----+-------+ | 40 | hi | +----+-------+ | 5 | hi | +----+-------+ The query will delete rows with id# 20 and 40 on

Using build with a has_one association in rails

倾然丶 夕夏残阳落幕 提交于 2019-12-17 03:46:34
问题 In this example, I create a user with no profile , then later on create a profile for that user. I tried using build with a has_one association but that blew up. The only way I see this working is using has_many . The user is supposed to only have at most one profile . I have been trying this. I have: class User < ActiveRecord::Base has_one :profile end class Profile < ActiveRecord::Base belongs_to :user end But when I do: user.build_profile I get the error: ActiveRecord::StatementInvalid:

Using build with a has_one association in rails

末鹿安然 提交于 2019-12-17 03:46:32
问题 In this example, I create a user with no profile , then later on create a profile for that user. I tried using build with a has_one association but that blew up. The only way I see this working is using has_many . The user is supposed to only have at most one profile . I have been trying this. I have: class User < ActiveRecord::Base has_one :profile end class Profile < ActiveRecord::Base belongs_to :user end But when I do: user.build_profile I get the error: ActiveRecord::StatementInvalid: