foreign-keys

grails hasOne vs direct member variable

我是研究僧i 提交于 2019-12-03 11:08:47
问题 Let's say I have a grails domain class that looks like class Person { Address address } I could also declare it as class Person { static hasOne = [address:Address] } The second way would move the foreign key to the Address table rather than the person table. What are the practical benefits (or disadvantages) of doing this one way vs the other? As far as I understand, they will both use foreign keys, it's just a matter of where the foreign key lives. 回答1: If the foreign key exists on the

mysql alter int column to bigint with foreign keys

二次信任 提交于 2019-12-03 11:05:11
I want to change the datatype of some primary-key columns in my database from INT to BIGINT. The following definition is a toy-example to illustrate the problem: CREATE TABLE IF NOT EXISTS `owner` ( `id` int(11) NOT NULL AUTO_INCREMENT, `thing_id` int(11) NOT NULL, PRIMARY KEY (`id`), KEY `thing_id` (`thing_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ; DROP TABLE IF EXISTS `thing`; CREATE TABLE IF NOT EXISTS `thing` ( `id` int(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

MySQL's INSERT IGNORE INTO & foreign keys

和自甴很熟 提交于 2019-12-03 10:31:51
问题 Why in MySQL, INSERT IGNORE INTO does not change the foreign key constraint errors into warnings? I'm trying to insert a number of records into a table and I expect MySQL to leave out the ones that result in error, any error, and insert the rest. Does anyone have any suggestions? And the SET FOREIGN_KEY_CHECKS = 0; is not my answer. Because I expect the rows which defy the constraints not to be inserted at all. Thanks 回答1: [NEW ANSWER] Thanks to @NeverEndingQueue for bringing this up. It

Link To Foreignkey in Admin Causes AttributeError When Debug Is False

我只是一个虾纸丫 提交于 2019-12-03 10:14:29
问题 I have used the following code in my models.py file: Create hyperlink to foreignkey class ModelAdminWithForeignKeyLinksMetaclass(MediaDefiningClass): def __getattr__(cls, name): def foreign_key_link(instance, field): target = getattr(instance, field) return u'<a href="../../%s/%s/%s">%s</a>' % ( target._meta.app_label, target._meta.module_name, target.id, unicode(target)) if name[:8] == 'link_to_': method = partial(foreign_key_link, field=name[8:]) method.__name__ = name[8:] method.allow_tags

Multiple foreign keys to the same table

元气小坏坏 提交于 2019-12-03 09:51:48
问题 I have a reference table with all sorts of controlled value lookup data for gender, address type, contact type, etc. Many tables have multiple foreign keys to this reference table I also have many-to-many association tables that have two foreign keys to the same table. Unfortunately, when these tables are pulled into a Linq model and the DBML is generated, SQLMetal does not look at the names of the foreign key columns, or the names of the constraints, but only at the target table. So I end up

How to add on delete cascade and on update restrict using phpmyadmin?

怎甘沉沦 提交于 2019-12-03 09:28:58
问题 I want to add ' On delete cascade and on update restrict' on foreign keys through phpmyadmin user Interface instead of executing query. I generally use Heidisql control panel for doing these actions. And now I'm having hard time doing the same on phpmyadmin . Any idea? 回答1: In the tab where you define the table structure, you get the list of columns and their properties and underneath that there should be a link "relation view", between "print view", and "propose table structure." That's

Algorithms for Updating Relational Data

时光怂恿深爱的人放手 提交于 2019-12-03 09:02:53
What algorithms are known to perform the task of updating a database by inserting, updating, and deleting rows in the presence of database constraints? More specifically, say that before images of rows to be deleted, after images of rows to be inserted, and both images of rows to be updated are in memory. The rows might be for several tables. An exact sequence of updates is either not known or has not been preserved -- only the before images and the after images that the database must ultimately be made to reflect are known. The database contains primary key, foreign key, and unique index

django: prefetch related objects of a GenericForeignKey

心不动则不痛 提交于 2019-12-03 08:54:15
问题 Suppose I have a model Box with a GenericForeignKey that points to either an Apple instance or a Chocolate instance. Apple and Chocolate , in turn, have ForeignKeys to Farm and Factory , respectively. I want to display a list of Box es, for which I need to access Farm and Factory . How do I do this in as few DB queries as possible? Minimal illustrative example: class Farm(Model): ... class Apple(Model): farm = ForeignKey(Farm) ... class Factory(Model): ... class Chocolate(Model): factory =

Multiple Foreign Keys for a Single Record in Rails 3?

孤者浪人 提交于 2019-12-03 08:33:14
I am working on an app that will manage students enrolled in a course. The app will have users who can log in and manipulate students. Users can also comment on students. So three of our main classes are Student, User, and Comment. The problem is that I need to associate individual comments with both of the other models: User and Student. So I've started with some basic code like this... class Student < ActiveRecord::Base has_many :comments end class User < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base belongs_to :student belongs_to :user attr_accessible :comment

Django: merging objects

♀尐吖头ヾ 提交于 2019-12-03 07:54:26
问题 I have such model: class Place(models.Model): name = models.CharField(max_length=80, db_index=True) city = models.ForeignKey(City) address = models.CharField(max_length=255, db_index=True) # and so on Since I'm importing them from many sources, and users of my website are able to add new Places, I need a way to merge them from an admin interface. Problem is, name is not very reliable since they can be spelled in many different ways, etc I'm used to use something like this: class Place(models