foreign-keys

Mysql show create constraint?

不羁岁月 提交于 2019-12-05 05:26:55
Is there a easy way to query a table for its constraints(foreignkeys specificaly) like show create table, but for the constraints only? thanks, pvgoddijn To show only the foreign key constraints you can check the constraint_type in information_schema.table_constraints and get the affected columns in information_schema.key_column_usage via a join SELECT b.table_name, b.column_name, b.constraint_name, b.referenced_table_name, b.referenced_column_name FROM information_schema.table_constraints a JOIN information_schema.key_column_usage b ON a.table_schema = b.table_schema AND a.constraint_name = b

Grails - multiple belongsTo of same class with cascading deletion

别说谁变了你拦得住时间么 提交于 2019-12-05 05:22:21
This one is for the Grails users here. I asked it on the grails - user mailing list, but I figured since I've been fighting this for a couple of days I should cast as wide a net as possible. I'm having some difficulty with trying to model relationships between two objects of the same type in another object (different type) referencing the two objects. As an example of what I'm trying to do, suppose you're modeling relationships among family members. Any given relationship "belongsTo" two different family members. So: class Person { hasMany[relationships: Relationship] static mappedBy =

MySQL Error : #1005 - Can't create table (errno: 150) When I try create more than 1 FK

久未见 提交于 2019-12-05 05:09:37
I have this table: CREATE TABLE IF NOT EXISTS `produtos` ( `id` int(11) NOT NULL auto_increment, `idcatprodutos` int(11) NOT NULL, `idcategoria` int(11) NOT NULL, `idmarca` int(11) NOT NULL, `nome` varchar(100) NOT NULL, PRIMARY KEY (`id`), KEY `FK_produtos_2` (`idcatprodutos`), KEY `FK_produtos_3` (`idmarca`), KEY `FK_produtos_4` (`idcategoria`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC AUTO_INCREMENT=39 ; and this table: CREATE TABLE IF NOT EXISTS `sugestoes` ( `id` int(11) NOT NULL auto_increment, `idproduto` int(11) NOT NULL, `idsugestao1` int(11) NOT NULL, `idsugestao2`

How can I create a foreign key on a column, every record of which may refer to a column in one of several tables?

佐手、 提交于 2019-12-05 05:06:45
I'm in the process of creating a social network. It has several entities like news, photo, which can have comments. Since all comments have the same columns and behave the same way, and the only difference is their type — news, or photo, or something else to be added in the future — I decided to create one table for all comments with a column named type . It worked perfectly until I decided to add foreign keys to my database schema. The comment table have a column parent , which refers to id of news or photo table, depending on the column type . The problem is, I can't add a foreign key which

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails - Laravel

空扰寡人 提交于 2019-12-05 04:58:10
I know that this question has already been asked, but I still can't find what I'm doing wrong. I'm using the framework Laravel. I have 2 tables (Users and Locations). When I want to create a User, I get the error message: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails ( festival_aid . users , CONSTRAINT fk_users_locations1 FOREIGN KEY ( location_id ) REFERENCES locations ( location_id ) ON DELETE CASCADE ON UPDATE NO ACTION) (SQL: insert into users ( user_id , user_email , location_id ) values (?, ?, ?)) (Bindings: array (

How to enable foreign key cascade delete by default in SQLite?

点点圈 提交于 2019-12-05 04:51:45
SQLite v3.7.5 Is there a way to enable SQLite Foreign Keys with cascade delete enabled by default? Given the following example: CREATE TABLE [Parent] ( [ParentId] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, [Name] VARCHAR(50) UNIQUE NOT NULL ); CREATE TABLE [Child] ( [ChildId] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, [ParentId] INTEGER NOT NULL, [Name] VARCHAR(50) NOT NULL, FOREIGN KEY(ChildId) REFERENCES Child(ParentId) ON DELETE CASCADE ); The only way I've been able to enable the cascade delete is to execute the PRAGMA foreign_keys = true command before a transaction: using( var conn =

DJANGO: How to list_display a reverse foreign key attribute?

谁说我不能喝 提交于 2019-12-05 04:42:53
I'm building a web app that tracks what library books a person checks out. I have the following models: class Person(models.Model): name = models.CharField(max_length=100) def __unicode__(self): return self.name class Book(models.Model): name = models.CharField(max_length=100) person = models.ForeignKey(Person) checkout_date = models.DateTimeField('checkout date') def __unicode__(self): return self.name On the Admin's "change list" page for Person, I would like to show what books that person has. Is this something that can be done? If so, how? admin.py class BookAdmin(admin.ModelAdmin): list

Foreign keys vs partitioning

蓝咒 提交于 2019-12-05 04:41:21
Since foreign keys are not supported by partitioned mySQL databases for the moment, I would like to hear some pro's and con's for a read-heavy application that will handle around 1-400 000 rows per table. Unfortunately, I dont have enough experience yet in this area to make the conclusion by myself... Thanks a lot! References: How to handle foreign key while partitioning Partitioning mySQL tables that has foreign keys? Well, if you need partitioning for a table as small as 400.000 rows get another database than MySQL. Seriously. By modern standards any table below 1.000.000 rows is normally

How to give foreign key a name in RoR 3?

感情迁移 提交于 2019-12-05 03:01:40
问题 How can I give foreign key a name in RoR? I use following command to give foreign key: rails generate scaffold Table2 id:integer Table1:references This command adds foreign key of Table1 in Table2 but with default name that is Table1_id . So how can I give custom name to it for example my_table_f_key instead of Table1_id . I'm using Ruby 1.9.2 and Rails 3.0.3. Edit:- In my project.rb model: belongs_to :own, :class_name => User In my user.rb model: has_many :owned_projects, :class_name =>

Null foreign key, in ManyToOne relation using hibernate [4.1.1] annotations

二次信任 提交于 2019-12-05 02:52:37
问题 I am trying to persist a one-to-many and a many-to-one relation using Hibernate 4.1.1 but the foreign key is always NULL . There are two entities: Account and Client . A Client could have multiple Accounts while an Account has exactly one Client . Here are the classes (only what matters): Account.java @Entity @Table(name = "account") public class Account implements Serializable { private Client client; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id") public long getId(