foreign-key-relationship

Mysql errno 150 trying to create table with foreign key references

不羁岁月 提交于 2019-12-05 23:41:17
I'm trying to create a table in mysql with a foreign key reference, like this: In database A: CREATE TABLE replication ( id varchar(255) NOT NULL PRIMARY KEY, uid varchar(255) NOT NULL, value int(11) NOT NULL, FOREIGN KEY (uid) REFERENCES databaseB.users(username) ); In database B i have a table named users like this: +-----------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------------+--------------+------+-----+---------+-------+ | id | varchar(255) | NO | | NULL | | | username | varchar(255) | NO | PRI | NULL | | +-------------

Can not delete or modify or see same table foreign key constraint

余生颓废 提交于 2019-12-05 22:42:48
A same table foreign key constraint in my database is not accessible. I can not drop it, disable it, add it back, ... How do I remove it and re-add it? Note: I have several versions of my database all created with the same script. Only in one I see this behavior. In others, this key is easily added and removed. Many thanks. Here is some scripts I ran and the result: At some point in the past i ran the following script: ALTER TABLE Recipe ADD CONSTRAINT FK_Recipe_DuplicateOfRecipeId_Recipe_Id FOREIGN KEY (DuplicateOfRecipeId) REFERENCES Recipe (Id) ; now running ALTER TABLE Recipe DROP

mysql export sql dump alphabatically,which cause foreign key constraints error during import

非 Y 不嫁゛ 提交于 2019-12-05 20:13:17
问题 I have 10 tables in my database(MySQL). two of them is given below tbl_state state_id |int(10) |UNSIGNED ZEROFILL auto_increment state_name |varchar(40) tbl_city city_id |int(10) |UNSIGNED ZEROFILL auto_increment city_name |varchar(40) | state_code |int(10) | UNSIGNED ZEROFILL (FK reference with tbl_state.state_id) Foreign Key Constraint : tbl_city.state_code is references to tbl_state.state_id . now my problem is when I export all tables and import again then it gives foreign key constraint

how to return a json response based on database relationship using eloquent

回眸只為那壹抹淺笑 提交于 2019-12-05 19:33:58
I'm quite new to Laravel. I'm figuring out to work with Eloquent. Let's say I have 2 tables: categories and products. These two table have a one-to-many relationship. 1 category can have many products. I would like to return a JSON response which consists of an array of categories where each category has an array of products. It should look like this: [ {"name":"Category 1", "products": [ {"name":"Product 1","price":"2.50"}, {"name":"Product 2","price":"2.50"} ] }, {"name":"Category 2", "products": [ {"name":"Product 1","price":"1.95"}, {"name":"Product 2","price":"2.15"} ] } ] Models:

ServiceStack OrmLite How can I achieve automatic setting of foreign key/related properties?

房东的猫 提交于 2019-12-05 17:19:10
I have created the following example to test Foreign Keys and up to this point, it works well. What I would like to be able to do, is use this framework that I built to set the property of the relationship and have it Save the child object when the Parent is saved and automatically set the PrimaryKey and Foreign Key. The DataManager class exposes the Connection public class DataManager { DataManager() { OrmLiteConfig.DialectProvider = SqliteDialect.Provider; ConnectionString = SqliteFileDb; updateTables(); } private void updateTables() { using (var dbConn = OpenDbConnection()) { dbConn

Maintaining a foreign key relationship when inserting into tables with autoincrementing Id's

人走茶凉 提交于 2019-12-05 12:41:35
I have two tables: Defect and DefectData. Each Defect may or may not have one or many DefectData. As such DefectData has a DefectId column as a foreign-key. The Id in both tables is an autoincrementing identity. The problem I am having is that when I want to insert a new Defect and its DefectData the Defect is inserted first and gets an Id, but I don't know what that Id is to give to DefectData. My solution is to then select from defects matching inserted data to get the Id. Insert Defect Get that defect's Id Insert DefectData(zero or many) with Id from 2. Setting IdentityInsert on then

Entity Framework 4, inserting with foreign key value not ID

﹥>﹥吖頭↗ 提交于 2019-12-05 10:07:47
This is pretty basic question I am sure but it's baffling me now :S I have 2 tables, Students and Courses with a foreign key constraint, Many Students to 1 Course. I store in my Students table a CourseId_FK reference, so when I am creating a Student how would I be able to do something like this? Students student = new Students(); student.Name = Bob; student.Course.CourseName = "Geography"; db.SaveChanges(); Right now, the above doesn't work and I have to resolve to student.CourseId = 22; Can anyone help me pls? Thanks David If you don't want to load the full course entity: Student student =

Django Generic Foreign keys - Good or Bad considering the SQL performance?

荒凉一梦 提交于 2019-12-05 03:24:55
I have a model A which contains a generic foreign key relation with limit choices to 3 other models(consider them as B , C and D ) in the same app. And I know the limitations of generic foreign keys that we can't use filter or get or anyother queryset operations. So to achieve something like this, A.objects.filter(generic_object__name="foo") I have to filter B, C and D's objects first as queryset, iterate over them and use the generic reverse relation to get the A objects as list(not queryset). I'm not sure about how it'll affect the SQL performace on database as the querying is not direct. PS

laravel - why function call with no parentheses?

核能气质少年 提交于 2019-12-05 01:28:15
I see this in a laravel tutorial : Auth::user()->item; where item is a function, inside models\User.php : function item() { return $this->hasMany('Item', 'owner_id'); } where Item is for models\Item.php So why the parentheses is not needed when item function is called ? Like : Auth::user()->item(); If I put the parentheses, the browsers goes crazy and crash. Also, if I rename Item.php to Item2.php, rename class Item to Item2, and I do hasMany('Item2', 'owner_id') , it won't work. But why ? Where does 'Item' came from ? Thanks, Patrick Laravel uses the magic function __get to handle arbitrary

Backbone-relational hasmany best practices

断了今生、忘了曾经 提交于 2019-12-05 01:08:02
I am new to Backbone-relational, I am not sure what is the right way to use HasMany. I have a Parent model which have many children (by "many" I mean thousands of children). In order to avoid performance issue, I query children by their foreign key: /child/?parent=1 , instead of create a huge list of child_ids in Parent . But seems this is not the way Backbone-relational work. So I am wondering what is the right way to handle this. 1, Change my json api to include list of child id in parent, then send thousands of ids as Backbone-relational recommend: url = function(models) { return '/child/'