foreign-keys

Entity Framework on delete cascade

允我心安 提交于 2019-11-27 03:16:53
问题 I have problem with deleting related rows in Entity Framework 4.1. I have tables with relations Book 1<--->* BookFormats I have set the on delete cascade: ALTER TABLE [dbo].[BookFormats] WITH CHECK ADD CONSTRAINT [FK_BookFormats_Book] FOREIGN KEY([BookID]) REFERENCES [dbo].[Book] ([BookID]) on delete cascade The EDMX property Then, I want to remove the all BokFormats items related to my Book object: var originalBook = m.db.Book.First(x => x.BookID == bookId); originalBook.BookFormats.Clear();

How do I add a foreign key to an existing SQLite table?

▼魔方 西西 提交于 2019-11-27 03:02:04
I have the following table: CREATE TABLE child( id INTEGER PRIMARY KEY, parent_id INTEGER, description TEXT); How do I add a foreign key constraint on parent_id ? Assume foreign keys are enabled. Most examples assume you're creating the table - I'd like to add the constraint to an existing one. You can't. Although the SQL-92 syntax to add a foreign key to your table would be as follows: ALTER TABLE child ADD CONSTRAINT fk_child_parent FOREIGN KEY (parent_id) REFERENCES parent(id); SQLite doesn't support the ADD CONSTRAINT variant of the ALTER TABLE command ( sqlite.org: SQL Features That

Why use Foreign Key constraints in MySQL?

两盒软妹~` 提交于 2019-11-27 03:01:43
问题 I was wondering, What will be my motivation to use constraint as foreign key in MySQL, as I am sure that I can rule the types that are added? Does it improve performance? 回答1: Foreign keys enforce referential integrity. These constraints guarantee that a row in a table order_details with a field order_id referencing an orders table will never have an order_id value that doesn't exist in the orders table. Foreign keys aren't required to have a working relational database (in fact MySQL's

SQLAlchemy: get Model from table name. This may imply appending some function to a metaclass constructor as far as I can see

泄露秘密 提交于 2019-11-27 02:42:51
问题 I want to make a function that, given the name of a table, returns the model with that tablename . Eg: class Model(Base): __tablename__ = 'table' ...a bunch of Columns def getModelFromTableName(tablename): ...something magical so getModelFromTableName('table') should return the Model class. My aim is to use the function in a simple form generator I'm making since FormAlchemy does not work with python3.2 and I want it to handle foreign keys nicely. Can anyone give me any pointers on how to get

Django: UserProfile with Unique Foreign Key in Django Admin

我们两清 提交于 2019-11-27 02:18:48
问题 I have extended Django's User Model using a custom user profile called UserExtension . It is related to User through a unique ForeignKey Relationship, which enables me to edit it in the admin in an inline form! I'm using a signal to create a new profile for every new user: def create_user_profile(sender, instance, created, **kwargs): if created: try: profile, created = UserExtension.objects.get_or_create(user=instance) except: pass post_save.connect(create_user_profile, sender=User) (as

Cannot add foreign key constraint - MySQL ERROR 1215 (HY000)

南楼画角 提交于 2019-11-27 02:17:01
问题 I am trying to create database for gym management system, but I can't figure out why I am getting this error. I've tried to search for the answer here, but I couldn't find it. ERROR 1215 (HY000): Cannot add foreign key constraint CREATE TABLE sales( saleId int(100) NOT NULL AUTO_INCREMENT, accountNo int(100) NOT NULL, payName VARCHAR(100) NOT NULL, nextPayment DATE, supplementName VARCHAR(250), qty int(11), workoutName VARCHAR(100), sDate datetime NOT NULL DEFAULT NOW(), totalAmount DECIMAL

Disabling foreign key constraint, still can't truncate table? (SQL Server 2005)

ぐ巨炮叔叔 提交于 2019-11-27 02:04:15
问题 I have a table called PX_Child that has a foreign key on PX_Parent. I'd like to temporarily disable this FK constraint so that I can truncate PX_Parent. I'm not sure how this goes however. I've tried these commands ALTER TABLE PX_Child NOCHECK CONSTRAINT ALL ALTER TABLE PX_Parent NOCHECK CONSTRAINT ALL (truncate commands) ALTER TABLE PX_Child CHECK CONSTRAINT ALL ALTER TABLE PX_Parent CHECK CONSTRAINT ALL But the truncate still tells me it can't truncate PX_Parent because of a foreign key

MySQL RESTRICT and NO ACTION

北慕城南 提交于 2019-11-27 02:01:40
问题 What's the difference in a MySQL FK between RESTRICT and NO ACTION ? From the doc they seem exactly the same. Is this the case? If so, why have both? 回答1: From MySQL Documentation: https://dev.mysql.com/doc/refman/8.0/en/create-table-foreign-keys.html Some database systems have deferred checks, and NO ACTION is a deferred check. In MySQL, foreign key constraints are checked immediately, so NO ACTION is the same as RESTRICT . 回答2: They are identical in MySQL. In the SQL 2003 standard there are

How to I show a list of ForeignKey reverse lookups in the DJango admin interface?

独自空忆成欢 提交于 2019-11-27 01:51:14
问题 I have a couple of models: class Customer(models.Model): customer_name = models.CharField(max_length=200) def __unicode__(self): return self.customer_name class Meta: ordering = ('customer_name',) class Unit(models.Model): unit_number = models.IntegerField() rentable = models.BooleanField() owner = models.ForeignKey(Customer, related_name='units', blank=True, null=True) def __unicode__(self): return str(self.unit_number) class Meta: ordering = ('unit_number',) I have the admin interface

Mysql - Add auto_increment to primary key

狂风中的少年 提交于 2019-11-27 01:22:11
问题 I have a strange problem with mysql. I am trying to alter a table's column which is a primary key and has an auto_increment constraint defined on it. This is also a foreign key reference for multiple other tables. I need to change the length of this column in both , parent and all children. set foreign_key_checks=0; alter table Parent modify Identifier smallint(10) unsigned; alter table Child_1 modify FK_Identifier smallint(10) unsigned; alter table Child_2 modify FK_Identifier smallint(10)