I have a Model with an auto-incrementing primary key. I want to be able to create an object from that model without an id, throw it away, and not have that Column's id auto-increment. Is that possible?
Assigning MyModel(id=None) does not do that.
Assigning MyModel(id=0) does not work either. It results in a Key (id)=(0) already exists. response.
To clarify: I want to keep the Column as auto-incrementing, but disable that behavior in specific circumstances.
The goal is to have a MyModel instance not saved in the database which I can pass to the template and can treat just like any other. For example, I want to print myModel.attribute in a template, but without saving myModel or incrememting the Column's id counter.
This is Postgres/Python/Flask/SQLAlchemy.
The problem is the instance is being auto-flushed. Not including session.add(myModel) does not fix it, but here are three solutions.
session.autoflush=False
this isn't great because it applies to everything after that line
session.expunge(myModel)
this might throw an exception and needs to be wrapped in a try/except
from sqlalchemy.orm import make_transient and make_transient(myModel)
this is what I went with
Related
I have a customerID as a primary key (Autonumber) and when I put customerID on another table I left it as short text instead of obviously number data type and I only realised after I created my forms and queries. Is there a way of changing data type and still being able to update my table using the forms I created with having to delete relationship, fix and redo all my forms?
When I change the data type this message appears "Some data will be lost. the setting for the fieldsize property of one or more fields has been changed to a shorter size. if data is lost, validation rules may be violated as a result"
always create a new column and copy into it the data you're going to convert before trying the conversion, jic. new column name: customerID_bak. copy all customerID data into it and try the conversion. if something goes wrong, you still have the original data. i think you actually might not have an issue with your forms after the conversion.
I have an SQL Server table structure where in some columns are foreign keys that refer to different look up tables. These columns are created as NOT NULL and with DEFAULT value = 1 as the look up value for ID = 1 is the default value I want to assign in case NULL is passed while saving a record.
However, I got below error while trying to Add/Update/Delete records of this child table.
An attempt was made to remove a relationship between a (*) and a (*). However, one of the relationship's foreign keys (X) cannot be set to null.
I tried to search a lot but I got solution to change LINQ to SQL code or XML manually for resolving this. I cannot do that because in future when ever the table gets changed, I'll have to make sure the manual change is applied every time.
Also, I can not change my columns to accept NULL because that will impact other applications using the same table through LINQ To SQL.
I need more manageable solution for this.
I assume that you are trying to "reset" a reference to its default value by assigning null to it, expecting L2S to adopt the database default. However, as for L2S, you're only nullifying a foreign key. It's not aware of any defaults in the database.
So you have to change the reference by setting it to the default lookup object yourself. You could do that by adding a Reset() method to your classes, so you don't have to scatter this code all over the place.
I got this resolved by assigning default values to the Table class properties from Code it self. What I understood is this. LINQ to SQL does not take into consideration the default value set from the DB. Also, the column is NOT NULL. So being INT columns the default value that .NET assigned was 0 to these properties. Now, I don't have any master entry with ID = 0 in my database. Also, since the DB did not get a NULL value for this column, it does not attempt to apply the Default value at all.
So what I did was, before saving the record, if I find that if the control which is bound to this property is having a NULL value (Nothing is selected/ control is invisible etc) I assign the Default value from the code it self. This eliminates the need to fall back on the database to assign the default.
Now, everything is working fine. Thought to share this as I could not get any satisfactory answers from anywhere.
I am having trouble inserting data into a MySQL table using CakePHP. I am making a table that once a row is inserted, it should not be able to be overwritten. With the save method of CakePHP, if I try to enter something with the same primary key but different information, it will just update that entry instead of returning an error. I tried to unset the id for the model and also tried to do Model->create() before the call, but it still just overwrites the data.
Additionally, I have been trying to use the Model->query() method instead, but I cannot get it to properly check for errors. I want it to insert, but return an error message if the ID is already taken, so I tried this.
$insertQuery = ("INSERT INTO `students` VALUES ('{$id}', '{$lastname}', '{$firstname}', '')");
$this->Student->query($insertQuery) or die("error" .mysql_error());
However, the query command returns an array and not a truth value, so this will call die every time. I would appreciate any advice someone can give.
Your table should have only the Id as the primary key and it should be IDENTITY.
In CakePHP if you dont specify the Id in the form you create to submit your data it will create another record, even with the same values.
But you should revise your model to be sure that the id is not required or something like that.
I have a need for the database that is created by EF CodeFirst to support default values so that non-nullable values can be inserted into the database when a value is not provided.
For example, I'd like to add a default date to a date column using so that the date column doesn't require a value when something is inserted into it outside of the model.
I know I can control this with defaults in code when using the model to update, but the the reason for this is primarily for model Updates. I frequently use RedGate's SqlCompare to update structures, but structure updates often fail on non-nullable, non-default fields because the new column needs some value to validate the schema, and so the column addition fails. Default values (and nullables) would allow for this. Nullables work, but that introduce other issues in dealing with null values in code that I'd like to avoid unless the model requires it.
Is there any way to accomplish getting default values into columns with EF CodeFirst either using the Fluent API or Attributes?
Yes, you need to use the HasDatabaseGeneratedOption method with DatabaseGeneratedOption of Computed
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed)
The only limitation is that you cannot update the values from code.
I understood that every new row, causes the deletion of the rows that were there before?
What is the idea behind it? I don't believe that it is ..
So, what am i getting wrong?
Edit A
I have a form that adds a store to the Stores table. the store have a column named owner_id which is associated to the Users table through a belongsTo relationship.
There is also a table named stores_users that supposed to store the mangers for each store, using the HABTM relationship.
For this table there is a form with an email field, that connects the user to the store by saving the record directly to the stores_users table.
So, there is no full HABTM save anywhere, if I understand the term correctly.
So, my questions are:
Should I expect problems using it this way?
Can you advice me about how to it, if my method is not the proper way?
How can I use the stored data, using $this->User->find(...) to get all the stores that the user can manage?
yes, thats the default behavior of HABTM in cakephp
although this is not on "every row", but "every HABTM save".
this is working IF you always provide all HABTM values.
and with baked views according to the specifications for such HABTM this is all working out of the box.
if you change the default behavior (old ones get not deleted) you will need to make sure that there are no duplicates. there are behaviors out there, I think, which try to accomplish that.
but I would recommend for you to build your forms the way that the default behavior of cake can do its job.
Example:
IS: 1,3,6 (in DB for this key)
NEW: 2,3,6 (coming from form)
(cake deletes 1,3,6 and adds 2,3,6)
=> overall result (forgetting about different primary keys): "1" deleted, "2" added
so it might not be the most resource sparing way but its sure the easiest and fastest.