foreign-key-relationship

“Integrity constraint violation: 1062 Duplicate entry” - but no duplicate rows

牧云@^-^@ 提交于 2019-12-12 11:34:40
问题 I'm converting an app from native mysqli calls to PDO. Running into an error when attempting to insert a row into a table with a foreign key constraint. Note: this is a simplified test case and should not be copy/pasted into a production environment. Info PHP 5.3, MySQL 5.4 First, here are the tables: CREATE TABLE `z_one` ( `customer_id` int(10) unsigned NOT NULL DEFAULT '0', `name_last` varchar(255) DEFAULT NULL, `name_first` varchar(255) DEFAULT NULL, `dateadded` datetime DEFAULT NULL,

How to set up a table with a recursive foreign key and a relationship declaratively in SQLAlchemy?

南楼画角 提交于 2019-12-12 09:27:35
问题 Suppose I have a table "nodes" where I store a tree. Each node has a primary key id and a column parent_id. Of course, I want to access a parent attribute of each node instance, that is, a relation. One might try: import sqlalchemy, sqlalchemy.orm, sqlalchemy.ext.declarative engine = sqlalchemy.create_engine('sqlite:///PATHTOMYDATABASE', echo=True) Base = sqlalchemy.ext.declarative.declarative_base() class Node(Base): __tablename__ = "nodes" id = sqlalchemy.Column(sqlalchemy.Integer, primary

How to do an INSERT into tables with circular relationships (SQL SERVER)

我怕爱的太早我们不能终老 提交于 2019-12-12 05:54:15
问题 I'm dealing with a set of tables in a database that appear to have a circular relationship (see image). This is the ARTS database if that is of any help to anyone. A user signing on: a) must create a (insert into) session, which in turn needs a SessionStartTransactionID (=SignOnTransaction) b) a SignOnTransaction is a type of ControlTransaction c) a ControlTransaciton is a type of Transaction d) a Transaction needs a reference to an existing Session (along with Operator, etc.) Note: The

Entity Framework Code First unique index foreign key throws DbUpdate Exception

时光怂恿深爱的人放手 提交于 2019-12-12 05:29:38
问题 Here's the entites and context code: public class Family { public int FamilyID { get; set; } public String address { get; set; } public virtual ICollection<Member> FamilyMembers { get; set; } } public class Member { [Index("MemberUniqueID", IsUnique = true)] public int MemberID { get; set; } [StringLength(50)] [Index("MemberUniqueIndex", 1, IsUnique = true)] public String name { get; set; } [StringLength(50)] [Index("MemberUniqueIndex", 2, IsUnique = true)] public String surname { get; set; }

How to delete a row ONLY in parent table, which is referenced by a Foregin Key from the child table

喜夏-厌秋 提交于 2019-12-12 04:55:49
问题 I want to delete a row/tuple from a parent table, but it is throwing an error message because it has a FOREIGN KEY reference in its child table. However, in my case I want to delete the record only from the parent table and maintain the data in the child table . Is it possible to achieve this? I know the usage of ON DELETE CASCADE, but I want to know if there is a solution for the secenario I described? 回答1: It is possible with some agreements in your data. To maintain child table data you'll

how do I create a DbSyncForeignKeyConstraint to a table with a composite Primary Key

时光毁灭记忆、已成空白 提交于 2019-12-12 04:46:27
问题 I am trying to create a DbSyncForeignKeyConstraint to a table with a composite Primary Key but, I keep getting errors. Here is some sample code to demonstrate what I am doing: EXAMPLE TABLES: USE [TempTest] GO CREATE TABLE [dbo].[Users]( [UserId] [uniqueidentifier] NOT NULL, CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED ( [UserId] ASC ) WITH ( PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY] ) ON [PRIMARY] CREATE

CakePHP-2.0 How can i get the username from $allposts=$this->paginate('Post'); where user_id is foreign key of posts table?

喜欢而已 提交于 2019-12-12 04:35:45
问题 I'm using CakeDC-Users plugin. <?php class Post extends AppModel { public $useTable='posts'; public $belongsTo = array('User'); public $hasMany=array('Comment'); } I had to use paginate: $allposts=$this->paginate('Post'); I can get the user_id in this way: foreach ($allposts as $post) { debug($post['Post']['user_id']); But i need the username not the user_id. How can i get username? 回答1: The containble feature of CakPHP hides all associated models by default: http://book.cakephp.org/2.0/en

Why do I receive a django error when trying to create a model instance with a foreign key?

爱⌒轻易说出口 提交于 2019-12-12 03:31:14
问题 I am receiving the following error: ValueError: Cannot assign "u'ben'": "Entry.author" must be a "MyProfile" instance. From this line: form.author = request.session['username'] Note: Entry.author is a foreign key as seen below. models.py class MyProfile(models.Model): user = models.CharField(max_length=16) first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30) def __unicode__(self): return u'%s %s %s' % (self.user, self.firstname, self.lastname) class Entry

I can't delete row after append it a foreign key

旧城冷巷雨未停 提交于 2019-12-12 03:29:39
问题 I have two table second and third second_id(PRIMARY KEY) second_name 1 ......... 2 ....... 3 ......... third_id(PRIMARY KEY) third_name second_id(FOREIGN KEY for second.second_id) 1 ..... 1 2 ..... 1 3 ..... 1 4 ..... 2 5 ..... 2 Now i want to delete a row from second where second_id=2 ( DELETE FROM second WHERE second_id=2 ) but it does not work. It says Successful 0 row(s) affected What is more is, it happened after i append a foreign key to third.second_id (i added foreign key after table

How do I reference a composite primary key with a foreign key using MySQL

…衆ロ難τιáo~ 提交于 2019-12-12 03:16:15
问题 I'm attempting to setup a foreign key in table cell_lines that will reference the topographic_region column of the composite primary key in table topographic_regions . Each time I run the last three lines of code trying to add the foreign key, I receive Error Code 1215: cannot add foreign key constraint. Now, the foreign key column name ( topographic_region ) in cell_lines only matches one of the composite primary key column names in topographic_regions , the other composite primary key