foreign-keys

JPA not generating “on delete set null” FK restrictions

故事扮演 提交于 2019-11-30 18:57:08
I have two related clases JPA-annotated. Alarm and Status. One Alarm can have one Status. What I need is to be able to delete one Status and "propagate" a null value to the Alarms that are in that Status that has been deleted. That is, I need the foreign key to be defined as " on delete set null ". @Entity public class Alarm { @Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="sequence") @SequenceGenerator(name="sequence", sequenceName="alarm_pk_seq") private Integer id; @OneToOne(cascade=CascadeType.ALL) @JoinColumn(name="idStatus") private Status status; // get/set } @Entity

How to prevent self (recursive) selection for FK / MTM fields in the Django Admin

旧时模样 提交于 2019-11-30 18:36:37
Given a model with ForeignKeyField (FKF) or ManyToManyField (MTMF) fields with a foreignkey to 'self' how can I prevent self (recursive) selection within the Django Admin (admin). In short, it should be possible to prevent self (recursive) selection of a model instance in the admin. This applies when editing existing instances of a model, not creating new instances. For example, take the following model for an article in a news app; class Article(models.Model): title = models.CharField(max_length=100) slug = models.SlugField() related_articles = models.ManyToManyField('self') If there are 3

Relationships in Entity Framework Code First

和自甴很熟 提交于 2019-11-30 18:04:08
问题 yesterday I created database in Management Studio and now I want to create it in program using EF Code First. Here is link to my database: http://s11.postimg.org/6sv6cucgj/1462037_646961388683482_1557326399_n.jpg And what I did: public class GameModel { [Key] public int Id { get; set; } public string Name { get; set; } public DateTime CreationTime { get; set; } public DateTime StartTime { get; set; } public DateTime EndTime { get; set; } public string TotalTime { get; set; } public DateTime

SET CONSTRAINTS ALL DEFERRED not working as expected in PostgreSQL 9.3

不打扰是莪最后的温柔 提交于 2019-11-30 17:47:20
If I define tables a and b as follows: CREATE TABLE a(i integer); ALTER TABLE a ADD CONSTRAINT pkey_a PRIMARY KEY (i); CREATE TABLE b(j integer); ALTER TABLE b add CONSTRAINT fkey_ij FOREIGN KEY (j) REFERENCES a (i) MATCH SIMPLE ON UPDATE CASCADE ON DELETE CASCADE; INSERT INTO a(i) VALUES(1); And then do the following: START TRANSACTION; SET CONSTRAINTS ALL DEFERRED; INSERT INTO b(j) VALUES(2); INSERT INTO a(i) VALUES(2); COMMIT; It produces the error below. Why is SET CONSTRAINTS not having the desired effect? ERROR: insert or update on table "b" violates foreign key constraint "fkey_ij" SQL

Laravel 5.1: Enable SQLite foreign key constraints

烂漫一生 提交于 2019-11-30 17:41:40
In SQLite, foreign key constraints are disabled by default . What's the best way to configure Laravel 5.1's SQLite database connection to enable foreign key constraints? I don't see a way of doing this in ['connections']['sqlite'] in /config/database.php . Here's one solution. In the boot() method of App\Providers\AppServiceProvider , add: if (DB::connection() instanceof \Illuminate\Database\SQLiteConnection) { DB::statement(DB::raw('PRAGMA foreign_keys=1')); } Thanks to @RobertTrzebinski for this blog post regarding Laravel 4. For me using a the facade DB within App\Providers

How can I get the foreign keys of a table in mysql

五迷三道 提交于 2019-11-30 15:32:11
I am creating a class which, takes a table from a database, and displays it to a web-page, with as much functionality as possible. One of the things I would like to support, would be having the class detect which columns in the table have a foreign key constraint on them, so that it can then go to those tables, get all of their values and use them in a select-box which is called when you edit those fields, to avoid someone violating foreign key constraints, The main problem is discovering which fields have a foreign key constraint on them, and which tables they are pointing to. Does anyone

Get Foreign Key Value

感情迁移 提交于 2019-11-30 15:26:16
How can I get the foreign key values? I have a common vehicle model that links to the year, series, engine type, body style, transmission and drive train...all as foreign keys. I'd like to get the values of these fields for my app, but I'm stuck as to how I'd go about them. Any ideas will be highly appreciated. class Model(models.Model): model = models.CharField(max_length=15, blank=False) manufacturer = models.ForeignKey(Manufacturer) date_added = models.DateField() def __unicode__(self): name = ''+str(self.manufacturer)+" "+str(self.model) return name class Year(models.Model): ALPHA_NUMERIC

Django 1.9 drop foreign key in migration

旧街凉风 提交于 2019-11-30 15:19:53
问题 I have a Django model that has a foreign key to another model: class Example(models.Model) something = models.ForeignKey(SomeModel, db_index=True) I want to keep the underlying DB column as a field, but to get rid of the foreign key constraint in the database. So the model will change to: class Example(models.Model): something_id = models.IntegerField() And, to be clear, something_id is the column that Django had created for the foreign key field. I do not want to drop the column and re

Django: How to get data connected by ForeignKey via Template?

一个人想着一个人 提交于 2019-11-30 14:55:36
问题 Since a few weeks I am learning Python and Django. Up to this point it has been enough to read the questions and the answers of other users.But now the moment of my first own question has come. I will try to describe my problem as best i can. My problem is that I cant query or get the data I want. I want to get the url of the first object of class Image which is associated by ForeignKey to a Gallery, which is associated by ForeignKey to the class Entry. Here the models.py so far: class

Specify ON DELETE NO ACTION in ASP.NET MVC 4 C# Code First

旧时模样 提交于 2019-11-30 14:32:38
问题 How do I specify ON DELETE NO ACTION Foreign Key Constraint in my model designs? At present, I have: public class Status { [Required] public int StatusId { get; set; } [Required] [DisplayName("Status")] public string Name { get; set; } } public class Restuarant { public int RestaurantId { get; set; } [Required] public string Name { get; set; } [Required] [EmailAddress] public string Email { get; set; } [Required] public string Telephone { get; set; } [Required] public int StatusId { get; set;