foreign-keys

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

这一生的挚爱 提交于 2019-12-01 17:20:23
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 following: public class A { private long id; private long idOfB; // getters and setters skipped } I

Why 'foreign key constraint fails' when foreign key exists?

岁酱吖の 提交于 2019-12-01 16:56:52
I have a simple query UPDATE `t_timecard_detail` SET `timeoff_request_id` = 'adad8e0d-c22b-41c3-a460-6cf982729299' WHERE `id` = 'cfc7a0a1-4e03-46a4-af89-069a0661cf55'; which gives this error ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`demo1_timeapp`.`t_timecard_detail`, CONSTRAINT `timeoff_request_id_refs_id_48fe5c4` FOREIGN KEY (`timeoff_request_id`) REFERENCES `t_timeoff_request` (`id`)) constraint is CONSTRAINT `timeoff_request_id_refs_id_48fe5c4` FOREIGN KEY (`timeoff_request_id`) REFERENCES `t_timeoff_request` (`id`) though the ID 'adad8e0d-c22b

Handling database integrity

末鹿安然 提交于 2019-12-01 16:37:40
问题 I'm introducing database integrity using innodb constraints in the next version of my application. Everything goes well, but some of my tables have records with deleted references (dead records) and because of them I can't add constraints to the table. I am trying: ALTER TABLE `article` ADD FOREIGN KEY (`author_id`) REFERENCES `authors` (`id`) ON DELETE CASCADE; And I get: #1452 - Cannot add or update a child row: a foreign key constraint fails (`books`.<result 2 when explaining filename '

Handling database integrity

隐身守侯 提交于 2019-12-01 16:29:35
I'm introducing database integrity using innodb constraints in the next version of my application. Everything goes well, but some of my tables have records with deleted references (dead records) and because of them I can't add constraints to the table. I am trying: ALTER TABLE `article` ADD FOREIGN KEY (`author_id`) REFERENCES `authors` (`id`) ON DELETE CASCADE; And I get: #1452 - Cannot add or update a child row: a foreign key constraint fails (`books`.<result 2 when explaining filename '#sql-442_dc'>, CONSTRAINT `#sql-442_dc_ibfk_1` FOREIGN KEY (`author_id`) REFERENCES `authors` (`id`) ON

What is causing this sqlite foreign key mismatch?

拥有回忆 提交于 2019-12-01 16:05:40
I already checked out this question , and thought I had the answer - but then it didn't look right to me. I have the following pared down example: CREATE TABLE pipelines ( name VARCHAR NOT NULL, owner VARCHAR NOT NULL, description VARCHAR, PRIMARY KEY (name, owner), FOREIGN KEY(owner) REFERENCES user (id) ); CREATE TABLE tasks ( id INTEGER NOT NULL, title VARCHAR, pipeline VARCHAR, owner VARCHAR, PRIMARY KEY (id), FOREIGN KEY(pipeline) REFERENCES pipelines (name), FOREIGN KEY(owner) REFERENCES pipelines (owner) ); CREATE TABLE user ( id VARCHAR NOT NULL, name VARCHAR, password VARCHAR, PRIMARY

Why does referencing a SQLite rowid cause foreign key mismatch?

谁说我不能喝 提交于 2019-12-01 15:23:48
SQLite version 3.7.9 2011-11-01 00:52:41 sqlite> PRAGMA foreign_keys = 1; sqlite> CREATE TABLE foo(name); sqlite> CREATE TABLE bar(foo_rowid REFERENCES foo(rowid)); sqlite> INSERT INTO foo VALUES('baz'); sqlite> SELECT rowid, name FROM foo; 1|baz sqlite> INSERT INTO bar (foo_rowid) VALUES (1); Error: foreign key mismatch Why does this error occur? It is a DML error , but I don't know what's wrong because: foo exists. foo.rowid exists. foo.rowid is the primary key of foo and therefore constrained to uniqueness. bar.foo_rowid is one column, which matches the fact that foo.rowid is one column.

ForeignKey field related to abstract model in Django

送分小仙女□ 提交于 2019-12-01 15:16:45
I have this model: class BaseModel(models.Model): .... class Meta: abstract = True class ModelA(BaseModel): .... class ModelB(BaseModel): .... class MyExtModel(models.Model) myfield = models.ForeignKey(BaseModel) But this is not correct because I have BaseModel like Abstract . Infact I have an error when I try makemigration command. The error is: ERRORS: myapp.MyExtModel.myfield: (fields.E300) Field defines a relation with model 'BaseModel', which is either not installed, or is abstract. Is there a way to use an abstract base model? I also tried to use: myfield = models.ForeignKey(BaseModel,

Django: Distinct foreign keys

倖福魔咒の 提交于 2019-12-01 15:12:46
问题 class Log: project = ForeignKey(Project) msg = CharField(...) date = DateField(...) I want to select the four most recent Log entries where each Log entry must have a unique project foreign key. I've tries the solutions on google search but none of them works and the django documentation isn't that very good for lookup.. I tried stuff like: Log.objects.all().distinct('project')[:4] Log.objects.values('project').distinct()[:4] Log.objects.values_list('project').distinct('project')[:4] But this

Django models.py Circular Foreign Key

£可爱£侵袭症+ 提交于 2019-12-01 15:01:46
I have a django app which basically is just a photo album. Right now I have two models: Image and Album . Among other things, each Album has a foreign key to an Image to be its thumbnail and each Image has a foreign key to the Album it belongs in. However, when I try to use manage.py syncdb or manage.py sqlall I get errors saying the class not defined first in models.py isn't defined when it is used in the first class defined. models.py (abridged): from django.db import models import os class Album(models.Model): thumb = models.ForeignKey(Image, null=True, blank=True) class Image(models.Model)

has_one with two foreign keys?

江枫思渺然 提交于 2019-12-01 14:52:00
I have two classes Message and User. Message has sender_id and recipient_id both foreign keys for User. How to build relationship where I'll be able to get user for both sender and recipient, like @message.sender.name and @message.recipient.name I tried to do it by this way: class Message < ActiveRecord::Base belongs_to :sender, :class_name => 'User', :foreign_key => 'sender' belongs_to :recipient, :class_name => 'User', :foreign_key => 'recipient' end class User < ActiveRecord::Base has_many :recivied_messages, :class_name => 'Message', :foreign_key => 'recipient' has_many :send_messages,