foreign-keys

Flask foreign_keys still shows AmbiguousForeignKeysError

廉价感情. 提交于 2019-12-04 05:31:43
I have two foreign keys in an entity refering to another entity. Here is how it looks class Review(db.Model): __tablename__ = 'Review' id = db.Column(db.Integer, primary_key = True) user_id = db.Column(db.Integer, db.ForeignKey('User.id'), nullable=False) business_user_id = db.Column(db.Integer, db.ForeignKey('User.id'), nullable=False) user = db.relationship('User', foreign_keys=[user_id]) business_user = db.relationship('User', foreign_keys=[business_user_id]) and class User(db.Model): __tablename__ = 'User' id = db.Column(db.Integer, primary_key = True) reviews = db.relationship('Review',

For a composite foreign key, is a/why is a composite UNIQUE constraint in the referenced table required for a column combination with a primary key?

允我心安 提交于 2019-12-04 05:22:07
问题 I have a question regarding explicitly defining of the uniqueness of something. This relates to the creation of a composite foreign key. I've created an example below to try and make my question as clear as possible (I've included some data inserts for ease of testing). Each entry for [Table1] must have a unique [Name] . CREATE TABLE [Table1] ( [ID] INT IDENTITY NOT NULL PRIMARY KEY, [Name] NVARCHAR(255) UNIQUE NOT NULL CHECK(LTRIM(RTRIM([Name])) <> '') ); INSERT INTO [Table1]([Name]) VALUES

Restrict foreign key relationship to rows of related subtypes

不想你离开。 提交于 2019-12-04 05:11:11
问题 Overview: I am trying to represent several types of entities in a database, which have a number of basic fields in common, and then each has some additional fields that are not shared with the other types of entities. Workflow would frequently involve listing the entities together, so I have decided to have a table with their common fields, and then each entity will have its own table with its additional fields. To implement: There is a common field, “status”, which all entities have; however

error altering table, adding constraint foreign key getting error “Cannot add or update a child row”

一个人想着一个人 提交于 2019-12-04 05:08:48
问题 mysql> DESCRIBE questions; +----------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+--------------+------+-----+---------+----------------+ | id | int(255) | NO | PRI | NULL | auto_increment | | question | varchar(255) | NO | | NULL | | | type | char(1) | YES | | NULL | | +----------+--------------+------+-----+---------+----------------+ mysql> DESCRIBE answers; +--------------+--------------+------+-----+---------+------

Foreign key for multiple tables and columns?

穿精又带淫゛_ 提交于 2019-12-04 05:03:29
问题 I have been learning about Foreign keys and I wanted to know if the way I used it is correct in the example below: CREATE TABLE user( id INT(11) NOT NULL AUTO_INCREMENT, username VARCHAR(50) NOT NULL, password VARCHAR(20) NOT NULL, PRIMARY KEY (id) ); CREATE TABLE items( i_id INT(11) NOT NULL AUTO_INCREMENT, name TINYTEXT NOT NULL, price DECIMAL(8,2) NOT NULL, PRIMARY KEY (i_id) ); CREATE TABLE user_purchase( i_id INT(11) NOT NULL, name TINYTEXT NOT NULL, id INT(11) NOT NULL, FOREIGN KEY (i

how do I add a foreign key pointing to the same table using phpMyAdmin?

拜拜、爱过 提交于 2019-12-04 04:26:24
I have an existing InnoDB table which already has foreign keys pointing to different tables. But when I try to create a foreign key pointing to the Primary index, I get an error (check data type). The table is User with User_Id as the Primary. I want a foreign key Manager_ID which is a FK to User_Id. Both of INT Both of Length 10 Unsigned... But I still get a data check error...? Powerlord Make sure that Manager_ID is not set to NOT NULL . You have to allow nulls on that field, as the top-most person in the company will have no manager. I found a post over on the MySQL boards that might help.

Remove Foreign Key Relationships From All Tables

╄→尐↘猪︶ㄣ 提交于 2019-12-04 04:11:15
I have a database that has several tables. Many of the tables have fields with foreign key constraints. I want to truncate the tables and then repopulate them with new data, and I also want to take off the foreign keys, as some of the relationships have changed. basically, I want to build the FK constraints up from scratch again. How can I remove the current FK constraints from all tables? You can play with the information_schema. Take a look at this page http://dev.mysql.com/doc/refman/5.0/en/key-column-usage-table.html select concat('alter table ',table_name,' drop foreign key ',constraint

Hibernate foreign key with a part of composite primary key

谁说我不能喝 提交于 2019-12-04 03:36:39
I have to work with Hibernate and I am not very sure how to solve this problem, I have 2 tables with a 1..n relationship like this: ------- TABLE_A ------- col_b (pk) col_c (pk) [other fields] ------- TABLE_B ------- col_a (pk) col_b (pk) (fk TABLE_A.col_b) col_c (fk TABLE_A.col_c) [other fields] How can I manage this with Hibernate? I do not have any idea how to declare a foreign key that would contain a part of primary key. My database schema is generated from the Hibernate model. I have found two solutions to this problem. The first one is rather a workaround and is not so neat as the

Why did Rails 5 changed “index” to “foreign key”?

醉酒当歌 提交于 2019-12-04 03:25:28
If you had this in Rails 4: t.references :event, index: true Now you could use foreign_key instead of index in Rails 5. I don't quite understand WHY they decided to do this, since the functionality remains the same, what you're adding is an INDEX, not a FOREIGN KEY to that column. In Rails 5 - when we reference a model, index on the foreign_key is automatically created. Migration API has changed in Rails 5 - Rails 5 has changed migration API because of which even though null: false options is not passed to timestamps when migrations are run then not null is automatically added for timestamps.

Is it possible to have foreign key enforced without object-to-object mapping?

纵然是瞬间 提交于 2019-12-04 03:22:30
问题 Assuming the following mappings are provided: <class name="A" table="a_table"> <id name="id"/> <many-to-one name="entityB" column="fk_B" not-null="false" unique="true"/> </class> <class name="B" table="b_table"> <id name="id"/> </class> Java class: public class A { private long id; private B entityB; // getters and setters skipped } Is it possible to change the Hibernate mapping so that foreign key is still enforced and created by Hibernate upon startup , but class A would look like as the