relational-database

Mapping for self referencing entity in EF Code First

百般思念 提交于 2019-12-04 06:35:46
In my database I have a table Category, with columns Id, CategoryName, ParentCategoryId, where ParentCategoryId has a constraint on Category.Id. I'm using entity framework code first, where the entity looks like: public class Category { public long Id { get; private set; } public string CategoryName { get; private set; } public long? ParentCategoryId { get; private set; } public Category ParentCategory { get; private set; } public virtual ICollection<Category> SubCategories { get; private set; } } If I try to run a query against this, I get the exception: The relationship 'ComplaintModel.FK

How do I structure a SQL query to find an object that is the parent of two specific other objects?

故事扮演 提交于 2019-12-04 05:28:34
问题 Say I have 2 tables, called parent and child. A parent can have zero to many children, and a child can have 1 to many parents. How do I find all the parent elements that are parents of two specific children. For instance, say I have Parents [p_a, p_b, p_c, p_d], and children: [c_a, c_b] They have a structure as follows: p_a has c_a as a child p_b has c_b as a child p_c has both c_a and c_b as children p_d has no children How do I formulate a query to select p_c? If they had a structure where

What's the proper way to store this data in a MySQL schema?

强颜欢笑 提交于 2019-12-04 03:55:12
I have a movie in a MySQL database. A movie contains data attributes that will never change, such as the following: Barcode: 025192018626 Format: DVD Runtime: 121 min. Discs: 1 Title: 12 Monkeys Year: 1995 It's one single row in a table. But I want to give my user's complete customization over this information in case something is incorrect according to them or if they just want to modify how the data is displayed in some way. I don't care why, I just want to give my users an option to do what they want. Let's say User #1 wants to change the Title for them to be "12 Monkeys (Shelf 1)" and that

SQLAlchemy: How to conditionally choose type for column by depending on its backend

随声附和 提交于 2019-12-04 02:52:42
I want to use HSTORE type for a column if it uses PostgreSQL as its backend, or PickleType otherwise. The problem is that we cannot determine which backend will be used when schema is being defined (in Python). How can I determine this and conditionally choose the data type when the table actually is created on the backend database? You can accomplish something like this with TypeEngine.with_variant : from sqlalchemy.types import PickleType from sqlalchemy.dialects import postgresql HybridType = PickleType() HybridType = HybridType.with_variant(postgresql.HSTORE(), 'postgresql') This creates a

Applying column permissions for a table over a trigger

前提是你 提交于 2019-12-04 02:32:15
问题 For now, i have a table called members, which contains stuff, which is splitted in conact datas, bank datas.... Now, the admin should be able to create, update, delete users, which are saved in another table, which can only access the administrator. The users should get their own mysql user account and the admins should also be able to set permissions, like that users arent able to access bank datas. For now, I made a trigger, which creates and deletes the user accounts. Now I wanted to apply

Natural join if no common attributes

空扰寡人 提交于 2019-12-04 00:57:49
What will natural join return in relational algebra if tables don't have attributes with same names? Will it be null or the same as cross-product (Cartesian operator)? If there are no attributes in common between two relations and you perform a natural join , it will return the cartesian product of the two relations. A cartesian product of two tables will be returned.This is because when we perform any JOIN operation on two tables a cartesian product of those tables is performed and then based on any select condition in WHERE clause the resultant rows are returned.But here as there are no

Dynamic column names in view (Postgres)

爷,独闯天下 提交于 2019-12-03 23:15:54
问题 I am currently programming an SQL view which should provide a count of a populated field for a particular month. This is how I would like the view to be constructed: Country | (Current Month - 12) Eg Feb 2011 | (Current Month - 11) | (Current Month - 10) ----------|----------------------------------|----------------------|--------------------- UK | 10 | 11 | 23 The number under the month should be a count of all populated fields for a particular country. The field is named eldate and is a

How to relate 3 tables depending on event

◇◆丶佛笑我妖孽 提交于 2019-12-03 22:30:23
问题 I have a table that have information about different types of events that can be done by persons in two categories civil and worker so for each one of them I have their respective tables civil{ civil_id, name, age,telephone...} the primary key is civil_id worker{ worker_id, name, duty, department...} the primary key is worker_id then the event table has a list of all possible events event {type_of_event} the primary key is type_of_event then I am planing to store information in other table

SQL database structure for Like and DisLike [closed]

戏子无情 提交于 2019-12-03 21:53:13
I am new to web programming and need some expert advise. I am using SQL database for one of my website project. My website will display a list of topics with a Like and Dislike button. Logged in users will be able to click on the Like or dislike button for each topic. I am able to keep a count of likes and dislikes for each topics. So if a user clicks on a certain topic it will display the number of Likes and Dislikes for that particular topic, however what I am trying to achieve is when someone clicks on a particular Users profile it should display all the topics that user likes or dislikes.

Rails has_many through for has_many with multiple models

三世轮回 提交于 2019-12-03 21:39:36
What would be the best way to model the following situation: Word belongs_to :wordable, :polymorphic => true Phrase has_many :words, :as => :workable belongs_to :story Line has_many :words, :as => :wordable belongs_to :story Story has_many :lines has_many :phrases has_many :words, :through => :phrases has_many :words, :through => :lines I want to be able to do @story.words to get list of all words that are linked to a story either via lines or via phrases... Is that possible? Harish Shetty Try this: class Story has_many :lines has_many :phrases def words(reload=false) @words = nil if reload