foreign-keys

Using PostgreSQL, why doesn't Hibernate/JPA create cascade constraints?

坚强是说给别人听的谎言 提交于 2019-12-03 13:45:57
I have an entity Bar : @OneToMany(cascade = CascadeType.ALL, mappedBy = "bar") private Set<Foo> fooSet; And an entity Foo : @ManyToOne(optional = false) @JoinColumn(name = "bar_id") private Bar bar; Hibernate creates the foreign key constraint on foo.bar -> bar.id but it doesnt specify ON DELETE CASCADE . Why not? And is there any ways to achieve it? Alternatively I can add the ON DELETE CASCADE manually in the DB (and disable DDL generation), is this a good practice? And also, do I have to modify my code in some way to let Hibernate know that related records are deleted automatically by the

Why do we need to use Foreign Keys? [duplicate]

非 Y 不嫁゛ 提交于 2019-12-03 13:39:27
Possible Duplicate: Should I use foreign keys? Ok, let's assume we have two tables, users and comments . In comments we have a column comment_made_by_user_id and it means which user typed that particular comment. Why do we need to specify it as a foreign key? If we don't do that, it will still work. We specify primary keys, because it makes queries faster as far as I know (we need to search only for one row while when we don't have a primary key/index, we have to go through all rows). Is it just a good coding practise? When you use foreign keys you get: Data integrity faster queries. users:

How can I load HABTM-with-foreign-key relationships in my fixtures?

我们两清 提交于 2019-12-03 13:18:21
I have the following two models: School and User, and a HABTM relationship between them, with a join table. In this join table, the foreign key refering to the User table is not called user_id but student_id . class School < ActiveRecord::Base has_and_belongs_to_many :students, :class_name => "User", :join_table => "schools_students", :foreign_key => "student_id" end class User < ActiveRecord::Base has_and_belongs_to_many :studying_schools, :class_name => "School", :join_table => "schools_students", :foreign_key => "school_id" end I would like to create in my Users and Schools fixtures a

Entity Framework Code first - FOREIGN KEY constraint problem

霸气de小男生 提交于 2019-12-03 12:49:17
I'm new to EF code first principal and currently with no clue what to do.. I have 2 POCO classes.. public class Problem { public int ProblemID { get; set; } public int UserID { get; set; } public int CategoryID { get; set; } public int RatingID { get; set; } public string Title { get; set; } public string Description { get; set; } public double Latitude { get; set; } public double Longitude { get; set; } public int State { get; set; } public DateTime CreatedOn { get; set; } public virtual Rating Rating { get; set; } public virtual Category Category { get; set; } public virtual User User { get;

Django: How to follow ForeignKey('self') backwards

假装没事ソ 提交于 2019-12-03 12:25:29
问题 class Achievement(MyBaseModel): parent_achievement = models.ForeignKey('self', blank=True, null=True, help_text="An achievement that must be done before this one is achieved") # long name since parent is reserved I can do : Achievement.objects.get(pk="1").parent_achievement which is great. But how do I get all the children? Achievement.objects.get(pk="1").parent_achievement_set doesn't work (and probably should have some more notation around it), and I didn't see too much when searching. Is

Foreign keys must be Index in mySQL?

无人久伴 提交于 2019-12-03 12:16:25
I've just created my first mySQL table on my own (other than using Joomla, Wordpress, etc.) and I am MS SQL developer for years but normally I can easily create a foreign key in MS SQL but I came across a difficulty or lack of knowledge here. Here is my tables : users user_id int primary auto_increment username varchar(20) password varchar(20) posts post_id in primary auto_increment title varchar(100) message text user_id int When I try to add a foreign key to users which refers to posts->user_id , I cannot see the posts->user_id option in the option list Relation_view window on mySQL panel. I

Django Model Inheritance And Foreign Keys

僤鯓⒐⒋嵵緔 提交于 2019-12-03 11:33:39
Basically, I have a model where I've created a superclass that many other classes share, and then each of those classes has some unique features that differ from each other. Let's say class A is the superclass, and class B, C, and D are children of that class. Both class B and class C can have multiples of class D, however I've seen that it's best to put the foreign key relationship in class D, which then refers to its parent class. Now in other languages, I could simply say it has a ForeignKey relationship to class A, and then the language recognizes the classes' true type. However, I don't

Understanding Update and Delete Rules for Relationships in SSMS 2008

随声附和 提交于 2019-12-03 11:30:26
问题 I am confused about what means the update and delete rule in SQL Server 2008 Management Studio when we define foreign key constraints. I also did not find related help documents (e.g. F1 help). Here is the screen snapshot. Appreciate if anyone could describe what do they mean and recommend some related documents to read. :-) 回答1: The foreign key defines a parent - child relationship between two tables. The primary key in the parent table is the foreign key in the up to n child table rows. Now

Symfony2 Doctrine schema update fails

蓝咒 提交于 2019-12-03 11:21:06
I created database on my local machine. After moving my project to server I imported backup from local (because I had some important data there). Now,when I'm trying to update schema on my server it gives my this output: php app/console doctrine:schema:update --force Updating database schema... [Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException] An exception occurred while executing 'ALTER TABLE golf_course ADD CONSTRAINT FK_EC96E162F1503E2B FOREIGN KEY (golf_id) REFERENCES golf (id)': SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a

Can a table have two foreign keys?

我的未来我决定 提交于 2019-12-03 11:13:09
问题 I have the following tables (Primary key in bold . Foreign key in Italic ) Customer table ID---Name ---Balance--- Account_Name --- Account_Type Account Category table Account_Type ----Balance Customer Detail table Account_Name ---First_Name----Last_Name---Address Can I have two foreign keys in the Customer table and how can I implement this in MySQL? Updated I am developing a web based accounting system for a final project. Account Category Account Type --------------Balance Assets