foreign-keys

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

喜欢而已 提交于 2019-12-03 02:53:06
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 it possible? Fall into SQL? By default, django will call the reverse the model name, followed by "_set",

grails hasOne vs direct member variable

耗尽温柔 提交于 2019-12-03 02:42:41
Let's say I have a grails domain class that looks like class Person { Address address } I could also declare it as class Person { static hasOne = [address:Address] } The second way would move the foreign key to the Address table rather than the person table. What are the practical benefits (or disadvantages) of doing this one way vs the other? As far as I understand, they will both use foreign keys, it's just a matter of where the foreign key lives. If the foreign key exists on the address table, then that address can only have one person. If the foreign key is on the person table, multiple

Is string or int preferred for foreign keys?

对着背影说爱祢 提交于 2019-12-03 02:40:34
I have a user table with userid and username columns, and both are unique. Between userid and username , which would be better to use as a foreign key and why? My Boss wants to use string, is that ok? It looks like you have both a surrogate key ( int userId ) and a natural key ( char or varchar username ). Either column can be used as a Primary key for the table, and either way, you will still be able to enforce uniqueness of the other key. There are many existing discussions on the trade-offs between Natural and Surrogate Keys - you will need to decide on what works for you, and what the

java hibernate entity: allow to set related object both by id and object itself

て烟熏妆下的殇ゞ 提交于 2019-12-03 02:32:08
I've got a following Java class which is also a Hibernate entity: @Entity @Table(name = "category") public class Category { @ManyToOne @JoinColumn(name="parent_id") private Category parent; public Category getParent() { return parent; } public void setParent(Category parent) { this.parent = parent; } The category represents a node in a category tree. I'm implementing a webservice which allows to CRUD categories. For instance, the interface has the ability to create a category tree node and it passes the category id as a parameter. I want just to create a new Category object and persist it into

Drop foreign key only if it exists

僤鯓⒐⒋嵵緔 提交于 2019-12-03 02:19:42
I'm on a MySQL database. I'm doing this, but it doesn't work. ALTER TABLE `object` DROP FOREIGN KEY IF EXISTS `object_ibfk_1`; I've tried to put this IF EXISTS wherever I could. How can check if foreign key is exists before drop it? NikolaB If you want to drop foreign key if it exists and do not want to use procedures you can do it this way (for MySQL) : set @var=if((SELECT true FROM information_schema.TABLE_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = DATABASE() AND TABLE_NAME = 'table_name' AND CONSTRAINT_NAME = 'fk_name' AND CONSTRAINT_TYPE = 'FOREIGN KEY') = true,'ALTER TABLE table_name drop

Can a table have two foreign keys?

心已入冬 提交于 2019-12-03 01:37:26
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 Liabilities Equity Expenses Income Asset Asset_ID -----Asset Name----Balance---- Account Type Receivable

Force drop mysql bypassing foreign key constraint

强颜欢笑 提交于 2019-12-03 01:33:05
问题 I'm trying to delete all tables from a database except one, and I end up having the following error: Cannot delete or update a parent row: a foreign key constraint fails Of course I could trial and error to see what those key constraints are and eventually delete all tables but I'd like to know if there is a fast way to force drop all tables (as I'll be able to re-insert those I don't want deleted). Google aimed me at some site that suggested the following method: mysql> SET foreign_key

MySQL - Using foreign key as primary key too

偶尔善良 提交于 2019-12-03 01:11:15
I have table 1 with a primary key user_id and table 2 where user_id is a foreign key. Only 1 record per user_id can exist in table 2, and no record can exist without it. QUESTION: Can user_id in table 2 be both foreign and primary key at the same time, and if yes, is it a good idea, what are pros/cons? Yes, you can do this (and you should, from a database design point of view). However, consider what it means if user_id is the primary key on table 2. You are in effect saying that each row in table 2 corresponds to a user, but you already have a table where each row corresponds to a user: table

MySQL's INSERT IGNORE INTO & foreign keys

自闭症网瘾萝莉.ら 提交于 2019-12-03 01:03:28
Why in MySQL, INSERT IGNORE INTO does not change the foreign key constraint errors into warnings? I'm trying to insert a number of records into a table and I expect MySQL to leave out the ones that result in error, any error, and insert the rest. Does anyone have any suggestions? And the SET FOREIGN_KEY_CHECKS = 0; is not my answer. Because I expect the rows which defy the constraints not to be inserted at all. Thanks [NEW ANSWER] Thanks to @NeverEndingQueue for bringing this up. It seems MySQL has finally fixed this issue. I'm not sure which version this problem was first fixed in, but right

How to see table fields from foreign keys in Laravel

倾然丶 夕夏残阳落幕 提交于 2019-12-02 22:05:25
问题 I'm new at Laravel and not good with syntax. I want to see the values of another table through the foreign key(id of that table). https://ibb.co/pXRFRHn You can see in this picture I get ids under user & class. I want the titles associated with these ids. I have tables sections,users and a class. I use class_id & user_id as the foreign key in the section table. When I try to show data, I see the id, but I want the name & other fields extracted from that id. Controller public function index()