I have a model named service, Another model named Deal. In the deal Model, I have two fields. The first one is older_service, and the second field is new_service. Both are related to the same Model (service), but when I make the migrations, it throws the following error :
AssertionError: ForeignKey(<django.db.models.fields.related.ForeignKey>) is invalid. First parameter to ForeignKey must be either a model, a model name, or the string
'self'
Any Help, please.
you have tow foreignkey as same name
Related
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
I was wondering if there was a way to make all objects of a model be fields in another model. For example say I have objects that are the names Bob, Steve, and Matt. I would want those names to be a column in another table.
My friends,
How would I do to identify, in any given model, which fields have the parameter unique=True? And how about unique_together. Is there a way to programmatically identify fields with this characteristics?
Any instance from any model have a method called _get_unique_checks() that will return a list of tuples with all the fields that are marked as unique.
Similarly, any instance of any model have another method called _check_unique_together().
I use the Zend 2 Framework to build my web application. I implemented my database table models by this tutorial: http://framework.zend.com/manual/2.1/en/user-guide/database-and-models.html
I have a many-to-many relationship between two models in my database. To get data from them I googled and found this link: http://mattmccormick.ca/2010/04/24/how-to-easily-create-models-and-table-relationships-in-zend-framework/
The problem is that all the table models extends from Zend_Db_Table_Abstract in the example. I don't know how to get data from the models.
I have a table containing votings, every voting has a unique hash id. Every voting also has tags. Therefore I defined a table tags with all the tags available and a voting_tag_map where all many-to-many relationships are mapped.
What I have tried so far is the following, that's code from my VotingTable class:
public function getTagsByVoting($votingHash){
$select = $this->tableGateway->getSql()->select();
$select->from(array('v' => 'voting'))
->join('voting_tag_map', 'v.voting_id=voting_tag_map.voting_id')
->join('tags', 'voting_tag_map.tag_id=tags.tag_id');
$resultSet = $this->tableGateway->selectWith($select);
return $resultSet;
}
It says then:
Since this object was created with a table and/or schema in the constructor, it is read only.
Thats because of the from() method. If I delete the from() method, it says:
Statement could not be executed
Can anyone help me please?
Since this object was created with a table and/or schema in the constructor, it is read only.
This error is because you are trying to set the table name in the from clause, but it's already been set in the contructor of the TableGateway, and you can't change it once set.
If you really need to do this then you can extens AbstractTableGateway yourself then you won't have to add a string tablename to the contructor, but you don't really need to use an alias on your main table...
The SQL error you get when you comment out the from() method will be due to your referencing the votes table as it's alias 'v' in your join, when you are not using the alias v, try changing it to 'voting.XXX' from 'v.XXX'
Does anyone knows how to get table name from model name? Also I want to get all column names and its types of that model/table name. Is it possible to get such details of given model name?
Thanks.
Table Name
To get the table, see
$this->Model->table
Or check the model for the class variable $useTable. If that's undefined, then you can infer it from the name of the model:
$tableName = Inflector::tableize($this->Model->alias);
See the Inflector documentation for similarly useful methods.
Columns
Take a look at the schema method of the Model class. For example:
var_dump($this->Model->schema());