foreign-keys

Disappearing Foreign Keys in phpMyAdmin

喜欢而已 提交于 2019-12-03 07:46:20
I am creating a new table inside mysql and I am trying to add a foreign key constraint to one of the fields. CREATE TABLE `onlineorder` ( `receiptid` varchar(10) NOT NULL default '', `delivereddate` date default NULL, `cid` int(10) NOT NULL, `card#` int(10) default NULL, `expire` date default NULL, PRIMARY KEY (`receiptid`), FOREIGN KEY (receiptid) REFERENCES purchase ) ENGINE=MyISAM DEFAULT CHARSET=latin1; However, after it creates it, I go into phpMyAdmin and export the table. and it seems like the foreign key constraint has disappeared. CREATE TABLE `onlineorder` ( `receiptid` varchar(10)

Circular dependencies in foreign keys: use it or avoid it?

自作多情 提交于 2019-12-03 07:24:28
My application loads lots of data from a database into a complex data structure. The in-memory data structure ressembles the structure of the database, which means that if the database contains the following tables: table A, key is A1 table B, key is B1, one of the columns is a foreign key to [the key of] table A table C, key is C1, one of the columns is a foreign key to [the key of] table B Then I have classes A, B and C, and: a data member of B (B::m_a) is a pointer to A a data member of C (C::m_b) is a pointer to B This implies that if I load the database, that I have to load it in the

Can't create super user with custom user model in Django 1.5

不打扰是莪最后的温柔 提交于 2019-12-03 07:22:40
my goal is to create a custom user model in Django 1.5 # myapp.models.py from django.contrib.auth.models import AbstractBaseUser class MyUser(AbstractBaseUser): email = models.EmailField( verbose_name='email address', max_length=255, unique=True, db_index=True, ) first_name = models.CharField(max_length=30, blank=True) last_name = models.CharField(max_length=30, blank=True) company = models.ForeignKey('Company') ... USERNAME_FIELD = 'email' REQUIRED_FIELDS = ['company'] I can't create a super user because of the company field (models.ForeignKey('Company') (python manage.py createsuperuser). My

MySQL with Soft-Deletion, Unique Key and Foreign Key Constraints

放肆的年华 提交于 2019-12-03 07:12:30
问题 Say I have two tables, user and comment . They have table definitions that look like this: CREATE TABLE `user` ( `id` INTEGER NOT NULL AUTO_INCREMENT, `username` VARCHAR(255) NOT NULL, `deleted` TINYINT(1) NOT NULL DEFAULT 0, PRIMARY KEY (`id`), UNIQUE KEY (`username`) ) ENGINE=InnoDB; CREATE TABLE `comment` ( `id` INTEGER NOT NULL AUTO_INCREMENT, `user_id` INTEGER NOT NULL, `comment` TEXT, `deleted` TINYINT(1) NOT NULL DEFAULT 0, PRIMARY KEY (`id`), CONSTRAINT `fk_comment_user_id` FOREIGN

laravel migration best way to add foreign key

孤街醉人 提交于 2019-12-03 06:10:33
Simple question: I'm new to Laravel. I have this migration file: Schema::create('lists', function(Blueprint $table) { $table->increments('id'); $table->string('title', 255); $table->integer('user_id')->unsigned(); $table->foreign('user_id')->references('id')->on('users'); $table->timestamps(); }); I want to update it to add onDelete('cascade') . What's the best way to do this? Firstly you have to make your user_id field an index: $table->index('user_id'); After that you can create a foreign key with an action on cascade: $table->foreign('user_id')->references('id')->on('users')->onDelete(

Multiple foreign keys?

半腔热情 提交于 2019-12-03 06:09:18
I've got a table that is supposed to track days and costs for shipping product from one vendor to another. We (brilliantly :p) stored both the shipping vendors (FedEx, UPS) with the product handling vendors (Think... Dunder Mifflin) in a "VENDOR" table. So, I have three columns in my SHIPPING_DETAILS table that all reference VENDOR.no. For some reason MySQL isn't letting me define all three as foreign keys. Any ideas? CREATE TABLE SHIPPING_GRID( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'Unique ID for each row', shipping_vendor_no INT(6) NOT NULL COMMENT 'Foreign key to VENDOR.no for

Django suffix ForeignKey field with _id

走远了吗. 提交于 2019-12-03 05:51:32
Suppose I have a field f in my model defined as follows as a foreign key: f = models.ForeignKey(AnotherModel) When Django syncs database, the filed created in db will be called f_id , automatically suffixed with '_id'. The problem is I want this field in db named exactly as what I defined in model, f in this case. How can I achieve that? Simon Liu Well it turns out there's a keyword argument called db_column . If you want the field called 'f' in the database is just as simple as: f = models.ForeignKey(AnotherModel, db_column='f') Further reference: The name of the database column to use for

Polymorphic association foreign key constraints. Is this a good solution?

笑着哭i 提交于 2019-12-03 05:45:24
We're using polymorphic associations in our application. We've run into the classic problem: we encountered an invalid foreign key reference, and we can't create a foreign key constraint, because its a polymorphic association. That said, I've done a lot of research on this. I know the downsides of using polymorphic associations, and the upsides. But I found what seems to be a decent solution: http://blog.metaminded.com/2010/11/25/stable-polymorphic-foreign-key-relations-in-rails-with-postgresql/ This is nice, because you get the best of both worlds. My concern is the data duplication. I don't

A way to check if foreign key exists in SQL 2005

此生再无相见时 提交于 2019-12-03 05:28:16
问题 Is there an easy way to check if a foreign key exists for a column in a table? I am writing a script which will add the foreign key only if it does not exist. 回答1: You can use this script: IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_NAME]') AND parent_object_id = OBJECT_ID(N'[dbo].[MyTable]')) BEGIN -- do stuff END This can be done if you expand out the table and right click on an existing FK and choose script key as "DROP TO" and then you will get a

Foreign Key add fails in MySQL with Error Code 1005, number 150

狂风中的少年 提交于 2019-12-03 05:27:43
So I'm attempting to add a new foreign key to one of my tables as such: ALTER TABLE `UserTransactions`.`ExpenseBackTransactions` ADD CONSTRAINT `FK_EBTx_CustomAccountID` FOREIGN KEY (`CustomAccountID` ) REFERENCES `UserTransactions`.`CustomAccounts` (`CustomAccountID`) ON DELETE NO ACTION ON UPDATE NO ACTION, ADD INDEX `FK_EBTx_CustomAccountID` (`CustomAccountID` ASC) ; and I keep getting the following error: Error Code: 1005 Can't create table './UserTransactions/#sql-187a_29.frm' (errno: 150) I've done quite a bit of changes in the past to this and other tables, and this is the first time I