When i do the CTRL+S combo to save my work, the foreign key that i have created dissapears and is missing when i reopen the file. Upon pressing CTRL+S the status line at the bottom says delete complete. I dont understand what its deleting when im asking it to save.
I have tried remaking it over 10 times now. Nothing seems to work, the data types are the same, databases have been selected. I dont know what else to try.
The way i generate the foreign keys.
Draw association from 1 to the other, foreign key dialog pops up, i check that the child and parent are correct, press OK.
I recently ran into the same issue. My workaround was not using the CTRL+S button. Instead just use the regular save button.
While creating the foreign key relation change the index name to something else then the default, and try to save. This usually indicate the exact foreign key is in use
This happened when I was connecting the foreign key for a child table that already had the column meant to be a foreign key. I deleted the column from that table and let the "Foreign Key Constraint" window create the column for me. It will create it for you if you have under a "Child: Table name", in a "Foreign Key Constraint" window a text "ForeignKey (*)".
In case you save the object containing external references and open it in another EAP, which don't contain such object all the relations are not recovered. Couldn't this be your case?
Related
I am using Flask-Migrate==2.0.0. Its not detecting the changes correctly. Every time I run python manage db migrate it generates a script for all models although they have been added successfully in previous revisions. I have added two new columns to a table, migration revision is supposed to have only those two new columns instead all tables are added to it. Is there anything I am missing?
EDIT 1
Here is whats happening.
I added Flask_Migrate to my project.
python manage db init
python manage db migrate
python manage db upgrade
Flask-Migrate generated tables for models plus alembic_version table with having revision
985efbf37786
After this I made some changes. I added two new columns in one of my table and run the command again
python manage db migrate
It generated new revision
934ba2ddbd44
but instead of adding just only those two new columns, the revision contains script for all tables plus those two new columns. So for instance in my first revision, I have something like this
op.create_table('forex_costs',
sa.Column('code', sa.String(), nullable=False),
sa.Column('country', sa.String(), nullable=False),
sa.Column('rate', sa.Numeric(), nullable=False),
sa.PrimaryKeyConstraint('code', 'country', name='forex_costs_id'),
schema='regis'
)
The second revision also contains exactly the same code. I don't understand why if its already generated.
I googled it a little and my problems looks exactly like this https://github.com/miguelgrinberg/Flask-Migrate/issues/93 but I am not using oracle DB. I am using Postgresql. Also I don't know if it has any effect but I am not creating my tables in Default Public Schema, instead I am creating two new schemas (schema_a and schema_b) as I have a lot of tables(Around 100). So just to arrange them.
EDIT 2
The first problem seems to have resolved by adding
include_schemas=True
in env.py.
Now the new migration is not trying to create already existing tables again but it has some issues with foreign keys. Every time I create a new revision, it tries to remove the already existing foreign keys and then tries to add them. Logs looks like this
INFO [alembic.autogenerate.compare] Detected removed foreign key (post_id)(post_id) on table album_photos
INFO [alembic.autogenerate.compare] Detected removed foreign key (album_id)(album_id) on table album_photos
INFO [alembic.autogenerate.compare] Detected removed foreign key (user_id)(user_id) on table album_photos
INFO [alembic.autogenerate.compare] Detected added foreign key (album_id)(album_id) on table prodcat.album_photos
INFO [alembic.autogenerate.compare] Detected added foreign key (post_id)(post_id) on table prodcat.album_photos
INFO [alembic.autogenerate.compare] Detected added foreign key (user_id)(user_id) on table prodcat.album_photos
I have tried adding name to each Foreign Key constraint but that doesn't have any effect.
Thanks for coming back and providing your feedback after you solved the issue. I had grief with the same issue for 2 hours while using postgres
Btw, I would like to point out that you would have to include the include_schemas option in the block context.configure, like so:
context.configure(connection=connection,
target_metadata=target_metadata,
include_schemas=True,
process_revision_directives=process_revision_directives,
**current_app.extensions['migrate'].configure_args)
Setting search_path to public fixed this issue. I always thought that in addition to setting schema info explicitly on each model, we also need to add those schemas on search_path. I was wrong. Changing postgresql search_path is not necessary once schemas are defined explicitly on each model.
The search path means that reflected foreign key definitions will not
match what you have in your model. This only applies to foreign keys
because that's how Postgresql does it. Read through
http://docs.sqlalchemy.org/en/latest/dialects/postgresql.html#remote-schema-table-introspection-and-postgresql-search-path
for background. - Michael Bayer
Is there a way to create a foreign key constraint using Persistent's schema syntax with the Postgres backend? Or do I need to do this manually with SQL? Specifically, an ON DELETE CASCADE relationship such that when a HackDay is deleted, all of its child Projects are deleted:
HackDay
title Text
created UTCTime default=now()
votingClosed Bool default=false
deriving Show
Project
hackday HackDayId
title Text
creators Text
votes Int default=0
created UTCTime default=now()
deriving Show
Persistent does not currently have any built-in support for triggers, though it's something we've been wanting to add (simply lacking manpower). For now, you'll have to add the trigger manually.
I'm brand new to ASP.NET MVC (Literally just two days of learning on the fly). I'm trying to create a website using MVC, however, I've screwed something up in my solution.
I created this table that had foreign key constraints but I initially forgot to set up a primary key on the id field and forgot to set it identity specification to yes. So when I updated my DataModel and brought this table in, it showed every field as being the primary key and it showed the relationship to the other tables. So I went back to the database and changed the id field for the table to be the primary key and identity specification to yes.
I went back to the DataModel, tried to Update Model From Database, went to the refresh tab, expanded the tables, and selected the table I wanted to refresh, then click on "Finish".
The data model appeared to make the changes but it showed the relationships to the other tables twice.
So I thought I'd delete the table (entity) from the DataModel diagrahm and bring the table back in through Update Model From Database. But when I went back to the Update Model From Database...
Under the Add tab, the table isn't under the Tables
Under the Refresh tab, the table is listed, but when I select it and click on "Finish" the table doesn't reaapear in the diagrahm.
In the model browser...
Under Models
Entity Types, The entity isn't listed
Under Associations, The relationships no longer appear (obviously because the entity is gone).
Under Models.Store
Tables/Views, The table is listed.
Constraints, the relationships are shown.
I've tried dragging the table over to the diagrahm from the Model Browser, but that doesn't appear to work.
I've tried to right click in the diagrahm, add Entity but my table doesn't appear as a Base Type.
I've tried to google but I'm coming up with nothing. With this entity missing, I cannot even build my solution. Please help.
Try to delete the entity file and add it again. But you have to name it as previous entity and don't forget to delete the connectionString in web.config before add it.
If you don't know what I am saying just let me know.
I'm getting more and more frustrated with EF...
I have a table called ExtendedField with:
Record
DocRef
DocType
Name
Record is the primary key
DocRef and DocType are foreign keys used to identify which Ticket they belong to
Name is the key used by the "definition" table to define what the field actually is
So basically I need 2 associations:
One between Ticket and ExtendedField
on ExtendedField.DocRef=ticket.record
and
ExtendedField.docType=HeaderDocType
One between Definition on
ExtendedField.Name=Definition.FieldName
Then I still need Record to be the primary key so I can directly access the fields.
As near as I can tell this is impossible to do in Entity Framework. For every association all the keys need to be mapped together, whereas I need two keys for one association, 1 key for another one and the actual primary key wouldn't be used in any associations.
It doesn't appear that you can define an association between fields that aren't entity keys either.
So is there any way to do this? Am I missing something?
It's a v1, bro. I myself have had some major pain with mapping of key constraints in EF. I hear that better things are coming in v2.
Error 3007: Problem in Mapping Fragments starting at lines 186, 205: Non-Primary-Key column(s) [WheelID] are being mapped in both fragments to different conceptual side properties - data inconsistency is possible because the corresponding conceptual side properties can be independently modified.
I found several places on the web describing this error, but I simply don't understand them. (confused smiley goes here)
One
Two
Three
Four
There is something pretty fundamental here, I must be missing. Can you explain it, so that I understand it? Maybe using my real life example below?
Foreign key 1:N Wheels.Id -> Slices.WheelId
I add them to entity framework, and WheelId is not visible in the Slices-entity.
Doing some workaround (deleting the relationship from the db before adding tables to EF - then re-creating it and updating EF) I managed to get the WheelId to stay in Slices, but then I get the error mentioned at the top.
Since Slices.WheelId is an FK, you cannot expose it in your client model, period. There are ways to get the value, though.
var wheelId = someSlice.Wheels.ID;
Update In EF 4 you can do this by using FK Associations instead of independent associations.
Try to remove foreign property column from Entity set using entity model design it will solve your problem
For example
We have two tables one is customer and other one is order, using entity model design we added association between customers and orders when we do this Ado.net entity framework i will add navigation properties to both below tables.
Like
Customer.Orders - Here order is list
Order.Customer
One - Many relation.
So we need to remove property from with name CustomerId[Foreign key column] from Order entity set.
For reference:
http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/2823634f-9dd1-4547-93b5-17bb8a882ac2/
I was able to overcome this problem by the following steps:
right click the designer window
Select 'update model from database'
Select Add AND make sure that the 'Include foreign key columns in the model' checkbox is selected.
click on Finish...
I had set foreign keys up in the database but framework still wasn't pulling them in correctly. So I tried to add the association myself.
However, when I did this I would get a mapping error. It took me A WHILE but I figured out. What I did was set up the association using the entity toolbox association tool and then you have to double click on the association (1 to many) line and set the primary and foreign key there. Hopefully, this to help others who might have the same problem. I couldn't find the answer anywhere.
I had this problem for quite a different reason, and the message was slightly different; it didn't say "data inconsistency is possible because the corresponding conceptual side properties can be independently modified."
I have a table involved in my model with a binary column where I store image data. I only want this data returned when I need it (performance is a feature), so I split the table using a method similar to this. Later on, I added a property to that table, then updated the model from the database. The wizard added the property to both entity types that refer to the table with the added property. I had to delete it from one of them to solve the error.
I've had this happen because Entity Framework Update wizard mismapped some keys (or did not update?). As a result, some columns were mistakenly labeled as keys, while actual key columns were treated as plain columns.
The solution was to manually open EDMX file, find the entities, and update the keys.
Couldn't get any of the answer to work with EF6. The problem seems to be the framework doesn't import the foreign keys correctly as Associations. My solution was removing foreign keys from the tables, and then manually adding the associations using Entity Framework model, using the following steps: Entity Framework - Add Navigation Property Manually
For LinQ to Entities queries in EF1, my workaround for not having access to the foreign key as a property is with the following code, which does not produce a join query to the associated table:
dbContext.Table1s.FirstOrDefault(c => (int?)c.Table2.Id == null)
i.e, the generated SQL is:
...WHERE ([Extent1].[Table2Id] IS NULL)...
Solution is to allow deleting Rule = Cascade on Sql association.
Same thing as to be done on .edmx model, adding element to
association:
<Association Name="FK_Wheels_Slices">
<End Role="Wheels" Type= "your tipe here" Multiplicity="1">
<OnDelete Action="Cascade" />
</End>
</Association>
I had a table already mapped in EF. I added two more tables which had foreign keys in the previously added table. I then got the 3007 error.
To fix the error I deleted all three tables from the EDMX file, and then re-added them all at once together (via "Update Model from Database..."), instead of in stages.
I checked my Error List window and noticed I had errors in the model. Fixed them and all is well
in my case I solved this error by tick (include foreign key columns in the model)
- update Model from database
- tick (include foreign key columns in the model)
- finish