foreign-keys

Inserting new records with one-to-many relationship in sqlalchemy

感情迁移 提交于 2021-01-20 14:11:35
问题 I'm following the flask-sqlalchemy tutorial on declaring models regarding one-to-many relationship. The example code is as follows: class Person(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50)) addresses = db.relationship('Address', backref='person', lazy='dynamic') class Address(db.Model): id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(50)) person_id = db.Column(db.Integer, db.ForeignKey('person.id')) Now I'm wondering how to

How to add foreign key in sql server [closed]

独自空忆成欢 提交于 2021-01-20 13:33:10
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 days ago . Improve this question I have Types column in my Help Table and I want to add foreign key to the Id column in my HelpType table but not really sure what I'm doing wrong so. If I can get any suggestion or help will be really appreciated. ALTER TABLE Help ADD CONSTRAINT FK_Types FOREIGN KEY (Types)

Is it possible to “catch” the invalid value in MySQL when I get a foreign key constraint error?

ⅰ亾dé卋堺 提交于 2021-01-05 05:39:45
问题 I'm importing a CSV file of ~150k rows into a MySQL (InnoDB) table. At some point during the import, there's a failure because of a foreign key constraint error. It shows which column the failure is occuring on: Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`db`.`table`, CONSTRAINT `fk` FOREIGN KEY (`otherTableId`) REFERENCES `otherTable` (`otherTableId`)) Is it possible to "catch" what that value is? I haven't tried anything yet, because I don't know

Is it possible to “catch” the invalid value in MySQL when I get a foreign key constraint error?

て烟熏妆下的殇ゞ 提交于 2021-01-05 05:33:48
问题 I'm importing a CSV file of ~150k rows into a MySQL (InnoDB) table. At some point during the import, there's a failure because of a foreign key constraint error. It shows which column the failure is occuring on: Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`db`.`table`, CONSTRAINT `fk` FOREIGN KEY (`otherTableId`) REFERENCES `otherTable` (`otherTableId`)) Is it possible to "catch" what that value is? I haven't tried anything yet, because I don't know

Is it possible to “catch” the invalid value in MySQL when I get a foreign key constraint error?

穿精又带淫゛_ 提交于 2021-01-05 05:33:45
问题 I'm importing a CSV file of ~150k rows into a MySQL (InnoDB) table. At some point during the import, there's a failure because of a foreign key constraint error. It shows which column the failure is occuring on: Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`db`.`table`, CONSTRAINT `fk` FOREIGN KEY (`otherTableId`) REFERENCES `otherTable` (`otherTableId`)) Is it possible to "catch" what that value is? I haven't tried anything yet, because I don't know

Mapping foreign key to non primary surrogate key column in EF code first

余生长醉 提交于 2021-01-02 06:16:12
问题 public class A { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public virtual int Aid { get; set; } public virtual ICollection<B> B { get; set; } } public class B { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public virtual int Bid { get; set; } [Key] [Column(Order = 0)] [Required] Public virtual string BName {get ; set} [Key] [Column(Order = 1)] [Required] public virtual int Aid { get; set; } [ForeignKey("Aid")] public virtual A A { get; set; } public virtual ICollection<C>

Change type of Django model field from CharField to ForeignKey

↘锁芯ラ 提交于 2020-12-27 07:45:08
问题 I need to change the type of a field in one of my Django models from CharField to ForeignKey . The fields are already populated with data, so I was wondering what is the best or right way to do this. Can I just update the field type and migrate, or are there any possible 'gotchas' to be aware of? N.B : I just use vanilla Django management operations ( makemigrations and migrate ), not South. 回答1: This is likely a case where you want to do a multi-stage migration. My recommendation for this

Insert bulk data into two related tables with foreign keys from another table

喜你入骨 提交于 2020-12-27 06:20:18
问题 I have imported some data to a temp SQL table from an Excel file. Then I have tried to insert all rows to two related tables. Simply like this: There are Events and Actors tables with many to many relationship in my database. Actors are already added. I want to add all events to Events table and then add relation(ActorId) for each event to EventActors tables. (dbo.TempTable has Title, ActorId columns) insert into dbo.Event (Title) Select Title From dbo.TempTable insert into dbo.EventActor

Hibernate: foreign key without entity class, only by id

核能气质少年 提交于 2020-12-05 05:14:13
问题 I have a hierarchical entity, which references it self as a parent. I need to do the mapping only via ids, not via entity instances (the reason is too complicated to explain). So I defined the entity this way: class Item { @Id private String id; @ManyToOne(targetEntity = Item.class) @JoinColumn(name = "PARENT_ID", nullable = true) private String parentId; } This seems to work fine. The foreign key constraint is created correctly in database. But when I execute the following query: SELECT i

Hibernate: foreign key without entity class, only by id

元气小坏坏 提交于 2020-12-05 05:13:48
问题 I have a hierarchical entity, which references it self as a parent. I need to do the mapping only via ids, not via entity instances (the reason is too complicated to explain). So I defined the entity this way: class Item { @Id private String id; @ManyToOne(targetEntity = Item.class) @JoinColumn(name = "PARENT_ID", nullable = true) private String parentId; } This seems to work fine. The foreign key constraint is created correctly in database. But when I execute the following query: SELECT i